Home > OS >  How to save fragment instance on bottomNavigationView using navigation component
How to save fragment instance on bottomNavigationView using navigation component

Time:06-08

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.

CodePudding user response:

if you don't want to save your fragment to the backstack, use this line to nav_graph of this fragment

     <fragment
       android:id="@ id/fragment1"
       android:name="fragmentClassName" >
      <action
       android:id="@ id/action_Fragment1_to_Fragment2"
       app:destination="@id/Fragment2_id"
       app:popUpTo="@ id/your_root_graph_id"
       app:popUpToInclusive="true" />

CodePudding user response:

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.

  • Related