'selectedItemId doesn't work anymore after androidx.navigation:navigation library update
I have the following code:
activityBinding.bottomNav.selectedItemId = R.id.myFragment
that was working fine with androidx.navigation:navigation-* version 2.3.5 but is not working with version 2.4.2 and any other 2.4.* version.
I've searched everywhere but I haven't found a solution to make it work (also alternative code like menu.findItem(R.id.myFragment).isChecked = true didn't work).
Any idea of what I'm doing wrong?
Solution 1:[1]
Since the new library, if with the navigator you set a fragment as starting fragment and you select with selectedItemId the same fragment, the result will be that you'll click on it but nothing will happen.
Putting the starting fragment in the nav_graph to something else and then selecting the right one using selectedItemId make it working.
Solution 2:[2]
It is working fine for me I am using 2.4.1 version of androidx.navigation:navigation-*
navController.addOnDestinationChangedListener { _, destination, _ ->
when (destination.id) {
R.id.mainFragment, R.id.recentsFragment, R.id.favouriteFragment
-> {
// do your Code
}
else -> {
//do your code
}
}
Log.i("TLogs", "onCreate: ${bottomNavigationView.selectedItemId}")
}
You can check it according to the example I have done and attached.
Log is showing the current item Id
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Filnik |
| Solution 2 | Tariq Hussain |
