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!
May be you can do like this ( I did quickly so may be it could be better!!)
class ReSelectedListener(val navigationViewCallBack: NavigationViewCallBack) : NavigationView.OnNavigationItemSelectedListener {
var newItemSelected : MenuItem? = null
override fun onNavigationItemSelected(item: MenuItem): Boolean {
if(newItemSelected != null){
if(newItemSelected!!.itemId == item.itemId){
navigationViewCallBack.setOnNavigationItemReselectedListener(item)
}
}
newItemSelected = item
return true
}
}
interface NavigationViewCallBack {
fun setOnNavigationItemReselectedListener(item: MenuItem)
}
And after
class YourFragment :Fragment(), NavigationViewCallBack
and
navigationView.setNavigationItemSelectedListener(ReSelectedListener(this))
and implements callback method in YourFragment:
override fun setOnNavigationItemReselectedListener(item: MenuItem) {
TODO("Not yet implemented")
}