-1

I'm having the problem that when I trying to switch between the menu item. The menu item will not pointing to the correct icon. here is the flow when i found this problem

  1. starting at the (Home)fragment,then press on the second menu item (Features )
case R.id.nav_home:
    //home fragment transaction
    actionBar.setTitle("Home");
    HomeFragment fragment1 = new HomeFragment();
    FragmentTransaction fragmentTransaction1 = getSupportFragmentManager().beginTransaction();
    fragmentTransaction1.replace(R.id.content, fragment1, "");
    fragmentTransaction1.commit();
    break;

  1. second menu item (features) will go to features activity
case R.id.nav_features:
    //features fragment transaction
    startActivity(new Intent(DashboardActivity.this, FeaturesActivity.class));
    break;
  1. close features activity and back to (Home) fragment
onBackPressed(); 
  1. bottom navigation still pointing to second menu item (features).

how can I make the system point to the correct menu item ?enter image description here

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
qing
  • 67
  • 1
  • 10

1 Answers1

2

You should return a boolean for this method:

 @Override
 public boolean onNavigationItemSelected(@NonNull MenuItem item) {

    //...
 }

do this :

 case R.id.nav_home:
      //home fragment transaction
      actionBar.setTitle("Home");
      HomeFragment fragment1 = new HomeFragment();
      FragmentTransaction fragmentTransaction1 = 
      getSupportFragmentManager().beginTransaction();
      fragmentTransaction1.replace(R.id.content, fragment1, "");
      fragmentTransaction1.commit();
      return true; // add this line and remove break;

if you don't want to select icon after click, you can return false.

Ehsan msz
  • 1,774
  • 2
  • 13
  • 26
  • what I'm trying to say is , when i end the activity that pop up on (features ) , the system will back to the (home) fragment while the bottom navigation button still pointing to( features) , (Home) fragment should match with the home button ? – qing Nov 02 '19 at 06:26
  • @qing you can return `false` when *feature* is selected in `onNavigationItemSelected()` . – Ehsan msz Nov 02 '19 at 13:42