When I change fragment
my FAB (FloatingActionButton)
not change her image. I've got this method to change image:
private void fabImages(){
final NavHostFragment nhf = (NavHostFragment)getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment_content_main);
if(nhf != null){
Fragment fragmentActual = nhf.getChildFragmentManager().getFragments().get(0);
if(fragmentActual instanceof HomeFragment){
binding.appBarMain.fab.setImageResource(R.drawable.ic_add_auto_blanco);
}else if(fragmentActual instanceof GalleryFragment){
binding.appBarMain.fab.setImageResource(R.drawable.ic_save);
}
}
}
i'm using this activity to my app:
I apply this method in onCreate
and onResume
from my MainActivity
but the image not change, why?
UPDATE:
MainActivity
:
public class MainActivity extends AppCompatActivity{
private AppBarConfiguration mAppBarConfiguration;
private ActivityMainBinding binding;
private String horaString, fechaString;
private NombreTallerPreferencia nombreTallerPreferencia;
private PermissionHelper permissionHelper;
private static final int PICK_IMAGE = 1;
public static final String TAG = "logcat";
public static final String BARRA = "/";
private View view_imagen_perfil;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
setSupportActionBar(binding.appBarMain.toolbar);
fabImages(); //method to change image
DrawerLayout drawer = binding.drawerLayout;
NavigationView navigationView = binding.navView;
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
@Override
protected void onResume() {
super.onResume();
fabImages(); //method to change image
}