Agree with Nikolai!
I created my own gist too
To save the state after rotation, you need to add Activity to it:
@Override protected void onSaveInstanceState(Bundle outState) { outState.putInt("opened_fragment", bottomNavigation.getCurrentItem()); super.onSaveInstanceState(outState); }
and in onCreate , right after setting up BottomNavigationView :
final defaultPosition = 0; final int bottomNavigationPosition = savedInstanceState == null ? defaultPosition : savedInstanceState.getInt("opened_fragment", defaultPosition); bottomNavigation.setCurrentItem(bottomNavigationPosition);
The biggest plus of this proposition is: There are several types of listeners; this shows that the previous position of choice and the listeners react even when the position is set programmatically. Everything is written in the link, use if you need.
Boris Safonov
source share