Okay so my case is that for certain scenarios, one or multiple items in the bottom nav bar would have the following behaviour:
- The click for the item will show a popup
- the popup can be cancelled
- the clicked item will not navigate the user anywhere
- the clicked item will not navigate the user anywhere
- the clicked item should NOT be selected
All of my use cases are being done but even though no navigation is performed, the clicked item becomes selected. I need the bottom bar to stay on the state if no navigation was done.
binding.bottomNavigation.setOnItemSelectedListener {
when (it.itemId) {
R.id.referral_bottom_nav -> {
//handles when to navigate and when to show popup
true
}
}
}
How do I do this?
In Android, the BottomNavigationView's selected item is automatically updated when an item is clicked. This is the default behavior and it's not straightforward to change it. However, you can use a workaround to achieve your desired behavior.
In this code, we're storing the previously selected menu item in previousMenuItem. When an item is clicked, we check its id. If it's the id of the item that should show a popup and not update the selection, we reselect the previously selected item (if it's not null) and return false to not update the selection. If it's another item, we update previousMenuItem and navigate to the selected screen, then return true to update the selection.
Please replace R.id.referral_bottom_nav with the actual id of the menu item that should show the popup and not update the selection.