Search code examples
androidandroid-architecture-navigation

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

  • 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.