1

I have following fragments in my app

  • SignInFragment
  • HomeFragment (named AllCategoryFragment in my code)
  • SettingFragment (opens from BottomNavigationView, used for signing out)
  • Few others fragments

app screenshot

I am using popUpTo and popUpToInclusive to remove SignInFragment from the back-stack when navigating to HomeFragment and it works perfectly fine.

Code snippet for SignInFragment -> HomeFragment

 <fragment
        android:id="@+id/signInFragment"
        android:label="Log In">
        <action
            android:id="@+id/action_signInFragment_to_allCategoryFragment"
            app:destination="@id/allCategoryFragment"
            app:popUpTo="@id/signInFragment"
            app:popUpToInclusive="true" />
 </fragment>

I tried using similar logic to clear the back-stack when navigating from SettingFragment after signing out to SignInFragment. My desired goal is to exit the app if the user presses the back button when on SignInFragment. Pressing the back button when on SignInFragment takes me back to the previously present/navigated fragments.

Code snippet for SettingFragment -> SignInFragment

 <fragment
        android:id="@+id/settingFragment"
        android:label="App Settings">
        <action
            android:id="@+id/action_settingFragment_to_manageCategoriesFragment"
            app:destination="@id/manageCategoriesFragment" />
        <action
            android:id="@+id/action_settingFragment_to_signInFragment"
            app:destination="@id/signInFragment"
            app:popUpTo="@id/allCategoryFragment"
            app:popUpToInclusive="true" />
 </fragment>

HomeFragment is set as the start designation in the navigation graph.

One more important note, XML code written above to clear the back-stack after signing out works only if there is one instance of HomeFragment on the back-stack, else the user is able to go back to the previously present/navigated fragment.

Back-stack is cleared for this case, HomeFragment -> HistoryFragment -> SettingFragment

But fails if I have somethings like this, HomeFragment -> HistoryFragment -> HomeFragment -> SettingFragment. In this case, after logging out my back-stack looks like HomeFragment -> HistoryFragment, but should be cleared.

pvs
  • 54
  • 1
  • 7

1 Answers1

0

I found a solution.

Following 3 bullet points explain how my navigation is set up

  • Login fragment is the startDestination.
  • You log out from the Settings fragment.
  • There's only one navigation graph.

The solution

If you set popUpTo to the id of your navigation graph and popUpToInclusive to true when navigating to Login fragment from Settings fragment after logging out, the backstack will be completely cleared.

pvs
  • 54
  • 1
  • 7
  • Conditional screen like login page should not be set as start destination as suggested by official: https://developer.android.com/guide/navigation/navigation-principles#fixed_start_destination. Also you can check out my post here: https://stackoverflow.com/q/71247097/3466808. – Sam Chen Mar 26 '22 at 21:32
  • @SamChen, I followed your advice of not using conditional destination as the start destination. I believe my solution is different from what you have in your post. Thanks anyway, really appreciate it. – pvs Mar 27 '22 at 11:48