Menu item of NavigationView doesn't get checked if it was the last item checked

15 views Asked by At

I have a menu with a few items, which are displayed or hidden depending on if the user is logged in.

If the user has logged in, I show an item option to sign out. When this happens, I launch the home window and check the home item in the menu.

Let say the user is logged in and see these items:

  • Home
  • Sign out
  • Search

If the item checked is "Search" and the user signs out, then "Home" item is checked as expected. However, if the item checked is "Home" and the user signs out, then "Sign out" keeps checked.

Scenario 1:

"Search" is checked, then the user taps on "Sign out":

enter image description here

As expected, "Home" is checked:

enter image description here

Scenario 2:

"Home" is checked, then the user taps on "Sign out":

enter image description here

"Sign out" is checked, instead of "Home":

enter image description here

Why does this happen?

This is the relevant code:

protected void onCreate(Bundle savedInstanceState) {
    ...
    NavigationView navigationView = this.findViewById(R.id.navigation_view);
    navigationView.setNavigationItemSelectedListener(this);
    MainActivity.menu = navigationView.getMenu();
    ...
}

public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    int seleccion = item.getItemId();
    switch (seleccion) {
        ...
        case (R.id.menu_logout):
            MainActivity.menu.findItem(R.id.menu_inicio).setChecked(true);
            break;
        ...
    }
    ...
}

Thank you!

0

There are 0 answers