onBackPressed(); needs to also change icons in BottomNavView depending on what fragment user navigates back to

How can I get the BottomNvagiationView icons to switch onBackPressed(); depending on what fragment the user navigates back to?

This is how I have done it and it works, but causes images to come back duplicated because I have more than instance I suppose of that fragment already in the stack. That’s my thought… Not sure if it’s right or not, but also not sure what to do about it.

Also, I have tried to use onResume(); in each of the four fragments so that when the user navigates back to that specific fragment that BottomNavView icon is selected.

How can I solve this issue?

MainActivity

private BottomNavigationView.OnNavigationItemSelectedListener navigationItemSelectedListener = menuItem -> {         switch (menuItem.getItemId()) {             case R.id.nav_home:                 mSelectedFragment = new TabLayoutFragment();                 break;             case R.id.nav_notifications:                 seenNotification();                 mSelectedFragment = new NotificationsFragment();                 break;             case R.id.nav_profile:                 SharedPreferences.Editor editor = getSharedPreferences("PREFS", MODE_PRIVATE).edit();                 editor.putString("profileid", Objects.requireNonNull(FirebaseAuth.getInstance().getCurrentUser()).getUid());                 editor.apply();                 mSelectedFragment = new ProfileFragment();                 break;             case R.id.nav_save:                 mSelectedFragment = new AttendingEventFragment();                 break;         }          if (mSelectedFragment != null) {             getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, mSelectedFragment, null).addToBackStack(null).commit();         }          return true;     };  @Override     public void onBackPressed() {         if (mDrawer.isDrawerOpen(GravityCompat.START)) {             mDrawer.closeDrawer(GravityCompat.START);         }         super.onBackPressed();          Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container);         if (fragment instanceof HomeFragment) {             mBottomNavigationView.setSelectedItemId(R.id.nav_home);         } else if (fragment instanceof AttendingEventFragment) {             mBottomNavigationView.setSelectedItemId(R.id.nav_save);         } else if (fragment instanceof NotificationsFragment) {             mBottomNavigationView.setSelectedItemId(R.id.nav_notifications);         } else if (fragment instanceof ProfileFragment) {             mBottomNavigationView.setSelectedItemId(R.id.nav_profile);         }         super.onBackPressed();     } 

How I tried to remove one of the super.onBackPressed();. Code doesn’t work…

@Override public void onBackPressed() {     Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container);         if (mDrawer.isDrawerOpen(GravityCompat.START)) {             mDrawer.closeDrawer(GravityCompat.START);         } else {             if (fragment instanceof HomeFragment) {                 mBottomNavigationView.setSelectedItemId(R.id.nav_home);             } else if (fragment instanceof AttendingEventFragment) {                 mBottomNavigationView.setSelectedItemId(R.id.nav_save);             } else if (fragment instanceof NotificationsFragment) {                 mBottomNavigationView.setSelectedItemId(R.id.nav_notifications);             } else if (fragment instanceof ProfileFragment) {                 mBottomNavigationView.setSelectedItemId(R.id.nav_profile);             }         }         super.onBackPressed();     } 
Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.