In BottomNavigationView it is possible to set:
bottomNavigationView.setOnNavigationItemReselectedListener(item -> {
// do nothing on reselection
});
However for NavigationView this is not available. What is a good equivalent?
UPDATE
Thanks to @ande I implemented the following:
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
if (currentMenuItemId == item.getItemId()) {
navDrawer.close();
return true;
}
currentMenuItemId = item.getItemId();
NavigationUI.onNavDestinationSelected(item, navController);
navDrawer.close();
return true;
}
That works well for if I only navigate via the menu items. (Btw, I just implemented the Listener in my Activity and added it from there, no need for an extra class)
However, when I press the back button then I will be able to press the menu button for the current destination, as the menu item did not update on onBackPressed()
.
Update 2
navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
@Override
public void onDestinationChanged(@NonNull NavController controller, @NonNull NavDestination destination, @Nullable Bundle arguments) {
currentMenuItemId = destination.getId();
}
});
This solved it!