Avinta
Avinta

Reputation: 768

Clear complete back stack of navigation controller

I have an App with LogIn flow and several fragments from all of those i can access a navigation drawer that has an option to log out of the app. Instead of connecting every fragment to my splash screen i would like to reset the navigation to the splash screen on log out.

My code looks like this:

private void resetNavController() {
    mNavController.navigate(
            R.id.splashScreenFragment,
            null,
            new NavOptions.Builder().setPopUpTo(R.id.splashScreenFragment, true).build()
    );

It navigates back to the splash screen but does not pop the entire backstack. Also if i change the true to false it does not work as expected. Whtat do i have to do to pop the complete backstack?

Upvotes: 3

Views: 1089

Answers (1)

Rui
Rui

Reputation: 1016

I was also struggling with this until I tried to pop the backstack to my main graph.

So, I have a Global Action:

<action
    android:id="@+id/global_navigate_to_login"
    app:destination="@id/login_navigation_graph"
    app:launchSingleTop="true"
    app:popUpTo="@id/main_navigation_graph"
    app:popUpToInclusive="true">
    <argument
        android:name="startWithLogout"
        android:defaultValue="true"
        app:argType="boolean" />
</action>

The argument is just additional logic to logout the user in the login flow in the use case when the auth refresh token expires. The "login_navigation_graph" is a nested graph that contains the login flow. And the "main_navigation_graph" is the main graph of the app.

For invoking this Global Action:

supportFragmentManager
    .navController()
    .navigate(R.id.global_navigate_to_login)

The entire backstack is cleared and the user is redirected to the Login destination.

Upvotes: 2

Related Questions