Search code examples
androidandroid-fragmentsnavigationbottomnavigationview

How to save fragment instance on bottomNavigationView using navigation component


I am using the navigation component for navigation between bottom Navigation view fragments. But the fragment states are saving, it is creating everytime I visit that menu item.

As per this update - https://developer.android.com/jetpack/androidx/releases/navigation#2.5.0-alpha04 the component is saving the state.


Solution

  • Fragment state will be retained in Jetpack Navigation for only the views which have "id" set on them. Views which don't have any "id" will not retain their state. Also, you should use AppBarConfiguration with NavController. There are examples with AppBarConfiguration to look up.

    And last and foremost, don't use "app:popUpTo" & "app:popUpToInclusive" attributes in navigation graph xml file for actions. That will pop all the fragments, till that "app:popUpTo" fragment in back stack once the action is triggered.