Search code examples
androidandroid-navigationandroid-navigation-graph

Android Navigation component popUpTo taking back to popped up fragment


Please do not mark this as duplicate, I have read these SO question already but still, it is not working navigation component popUpTo bug Android navigation component popUpTo behaviour Android Navigation Component + Login Flow + Nested BottomNavigationView

I am using

def nav_version = "2.2.1"
    implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
    implementation "androidx.navigation:navigation-ui-ktx:$nav_version"

Scenario: Here is my app graph enter image description here

And this is my navigation code:

<fragment
        android:id="@+id/splashFragment"
        android:name="com.view.SplashFragment"
        android:label="SplashFragment" >
        <action
            android:id="@+id/action_splashFragment_to_loginFragment"
            app:destination="@id/loginFragment"
            app:popEnterAnim="@anim/slide_in_left"
            app:popExitAnim="@anim/slide_out_right"
            app:enterAnim="@anim/slide_in_right"
            app:exitAnim="@anim/slide_out_left"
            app:popUpTo="@id/loginFragment"
            app:popUpToInclusive="true"/>
        <action
            android:id="@+id/action_splashFragment_to_mainFragment"
            app:destination="@id/mainFragment"
            app:popEnterAnim="@anim/slide_in_left"
            app:popExitAnim="@anim/slide_out_right"
            app:enterAnim="@anim/slide_in_right"
            app:exitAnim="@anim/slide_out_left"
            app:popUpTo="@id/mainFragment"
            app:popUpToInclusive="true"/>
    </fragment>

When I press back button when I am on MainFragment or LoginFragment I am still able to navigate back to splashFragment. I have app:popUpTo and app:popUpToInclusive tags present already. I want my app not to navigate back to splashFragment


Solution

  • You're using the wrong id in your popUpTo

    As per the popUpTo guide:

    app:popUpTo tells the Navigation library to pop some destinations off of the back stack as part of the call to navigate(). The attribute value is the ID of the most recent destination that should remain on the stack.

    You can also include app:popUpToInclusive="true" to indicate that the destination specified in app:popUpTo should also be removed from the back stack.

    The popUpTo should point to the destination that is already on the back stack that you want to pop off the back stack. Therefore if you want to pop everything up to the splashFragment, you should use app:popUpTo="@id/splashFragment". If you want to pop everything up to and including the splashFragment, then you should use app:popUpTo="@id/splashFragment" and app:popUpToInclusive="true":

    <fragment
        android:id="@+id/splashFragment"
        android:name="com.view.SplashFragment"
        android:label="SplashFragment" >
        <action
            android:id="@+id/action_splashFragment_to_loginFragment"
            app:destination="@id/loginFragment"
            app:popEnterAnim="@anim/slide_in_left"
            app:popExitAnim="@anim/slide_out_right"
            app:enterAnim="@anim/slide_in_right"
            app:exitAnim="@anim/slide_out_left"
            app:popUpTo="@id/splashFragment"
            app:popUpToInclusive="true"/>
        <action
            android:id="@+id/action_splashFragment_to_mainFragment"
            app:destination="@id/mainFragment"
            app:popEnterAnim="@anim/slide_in_left"
            app:popExitAnim="@anim/slide_out_right"
            app:enterAnim="@anim/slide_in_right"
            app:exitAnim="@anim/slide_out_left"
            app:popUpTo="@id/splashFragment"
            app:popUpToInclusive="true"/>
    </fragment>