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
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.