Reputation: 343
I have a BottomNavigationView
setup with Navigation Component, and I want to intercept the handling of changing destinations on pressing an item, and prevent it from changing destination on a certain condition.
For example: Showing a dialog if the user is navigating to a destination and he's not signed in.
How do I do that?
I can't seem to be finding a listener that could be called OnDestinationChange
with some sort of boolean returned to inform the navigation component that it should go ahead with the navigation or not.
Upvotes: 1
Views: 432
Reputation: 1312
There is no way to do so using navigation component methods, but there is an easy workaround that doesn't break navigation flow. on your bottom navigation view get the menu and set onItemClickListener and return true if you don't want the navigation component to receive the click hence no navigation happens here is example
binding.bottomNav.menu.findItem(R.id.searchFragment)?.setOnMenuItemClickListener {
if(viewModel.isUserAuthorized()) {
false // here this will allow navigation component to consume the click
}
else {
showUnauthorizedUserDialog()
true // here this will prevent navigation component from consuming the click
}
}
additionally according to docs of onMenuItemClick
@return Return true to consume this click and prevent others from executing.
Upvotes: 3