Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 792
Avinta Avatar asked Feb 27 '26 15:02

Avinta


1 Answers

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.

like image 174
Rui Avatar answered Mar 02 '26 04:03

Rui



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!