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":
As expected, "Home" is checked:
Scenario 2:
"Home" is checked, then the user taps on "Sign out":
"Sign out" is checked, instead of "Home":
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!


