0

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.

Amit
  • 53
  • 4
  • I find out it's not about overide, Where ever I put navigationRailView.setSelectedItemId(), after that I get: savedInstanceState = null – Amit Jun 24 '22 at 05:57

2 Answers2

1

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);
Amit
  • 53
  • 4
0

try this

navigationRailView.setSelected(false);
Wowo Ot
  • 1,362
  • 13
  • 21