Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Navigation Component clear stack

I have navigated to a DialogFragment from Navigation drawer that connected to NavController. But when I navigate to another destination which I have set popUpTo and Inclusive of Dialog fragment it does not clear stack. How can I clear stack?

AM calling this method from LogoutDialog

findNavController().navigate(R.id.action_logoutDialog_to_auth_navigation)

Am using fragments and only one activity.

This is part of my navigation graph

 <fragment
        android:id="@+id/homeFragment"
        android:name="io.ui.home.HomeFragment"
        tools:layout="@layout/fragment_home" >
        <action
            android:id="@+id/action_homeFragment_to_auth_navigation"
            app:destination="@id/auth_navigation"
            app:popUpTo="@id/homeFragment"
            app:popUpToInclusive="true" />
        <argument
            android:name="fromLogin"
            app:argType="boolean"
            android:defaultValue="false" />
    </fragment>

    <dialog
        android:id="@+id/logoutDialog"
        android:name="io.ui.dialog.LogoutDialog"
        android:label="LogoutDialog" >
        <action
            android:id="@+id/action_logoutDialog_to_auth_navigation"
            app:destination="@id/auth_navigation"
            app:popUpTo="@id/logoutDialog"
            app:popUpToInclusive="true" />
    </dialog>

    <!-- auth navigation graph -->
    <navigation
        android:id="@+id/auth_navigation"
        app:startDestination="@id/loginFragment">
        <fragment
            android:id="@+id/loginFragment"
            android:name="io.ui.login.LoginFragment"
            android:label="fragment_login"
            tools:layout="@layout/fragment_login">
            <action
                android:id="@+id/action_loginFragment_to_OTPFragment"
                app:destination="@id/OTPFragment"
                app:popUpTo="@id/loginFragment"
                app:popUpToInclusive="true" />
            <action
                android:id="@+id/action_loginFragment_to_homeFragment"
                app:destination="@id/homeFragment" />
            <action
                android:id="@+id/action_loginFragment_to_resetPasswordFragment"
                app:destination="@id/resetPasswordFragment" />
        </fragment>

        <fragment
            android:id="@+id/OTPFragment"
            android:name="io.ui.login.OTPFragment"
            android:label="fragment_otp"
            tools:layout="@layout/fragment_otp">
            <action
                android:id="@+id/action_OTPFragment_to_homeFragment"
                app:destination="@id/homeFragment"
                app:popUpTo="@id/OTPFragment"
                app:popUpToInclusive="true" />
            <action
                android:id="@+id/action_OTPFragment_to_loginFragment"
                app:destination="@id/loginFragment"
                app:popUpTo="@id/OTPFragment"
                app:popUpToInclusive="true" />
        </fragment>
        <fragment
            android:id="@+id/resetPasswordFragment"
            android:name="io.ui.profile.PasswordFragment"
            android:label="fragment_reset_password"
            tools:layout="@layout/fragment_password" >
            <argument
                android:name="isReset"
                app:argType="boolean"
                android:defaultValue="false" />
        </fragment>

    </navigation>
like image 292
Mihango K Avatar asked Sep 01 '26 05:09

Mihango K


1 Answers

You're a looking for these attribute to put in your action.

  • The popUpTo attribute of an action "pops up" the back stack to a given destination before navigating. (Destinations are removed from the back stack.)

  • If the popUpToInclusive attribute is false or is not set, popUpTo removes destinations up to the specified destination, but leaves the specified destination in the back stack.

  • If popUpToInclusive is set to true, the popUpTo attribute removes all destinations up to and including the given destination from the back stack.

  • If popUpToInclusive is true and popUpTo is set to the app's starting location, the action removes all app destinations from the back stack. The Back button takes the user all the way out of the app.


EDIT: ALSO I'm adding this really great video which explains it with example https://www.youtube.com/watch?v=mLfWvSGG5c8

like image 143
Biscuit Avatar answered Sep 02 '26 18:09

Biscuit