How to update selected item when bottom navigation switch to Navigation Rail

403 Views Asked by At

I have a Bottom-Navigation in portrait Mode and a Navigation-Rail-View in landscape mode.

How can I update the selected item when the device is rotated?

I tried it with savedInstanceState, with override theonSaveInstanceState and onRestoreInstanceState.

But because I use savedInstanceState for other things too, overide the two above methods cause crash.

2

There are 2 best solutions below

0
Amit On

Eventually I found a solution.

But before that, I should mention that a better title for this question could be: How to share selection item between the BottomNavigation and NavigationRail.

Ok, solution:

Just replace setSelectedItemId with getMenu().getItem(itemIndex).setChecked(true)

Example:

Use:

bottomNavigationView.getMenu().getItem(indexOfItem2).setChecked(true);

Instead of:

bottomNavigationView.setSelectedItemId(R.id.item2);
0
Wowo Ot On

try this

navigationRailView.setSelected(false);