How do I call a component and its properties in the Fragment?

1

I needed to enter Fragment to execute this method Verifcar() on main Activity to check the condition and not show RecyclerView .

MainActivity.Java

public void Verificar(){
            if(pref.getBoolean(Constants.IS_LOGGED_IN,false)){

                recyclerView.setVisibility (View.VISIBLE);
    }else {

                recyclerView.setVisibility (View.INVISIBLE);
    }

}
    
asked by anonymous 24.05.2018 / 15:44

1 answer

0

You can call your Activity method within the Fragment using:

((SuaActivity)getActivity()).seuMetodoPublico();

If you have problems with cast of Activity, you can use:

Activity activity = getActivity();

if (activity instanceof SuaActivity){
 ((SuaActivity) activity).seuMetodoPublico();
} 
    
24.05.2018 / 16:00