0

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

MXC
  • 458
  • 1
  • 5
  • 21
  • Why do you have a splash fragment as the start destination of your graph? That is specifically not following the [Principles of Navigation](https://developer.android.com/guide/navigation/navigation-principles#fixed_start_destination) nor the [guide to Conditional Navigation for login](https://developer.android.com/guide/navigation/navigation-conditional#login). – ianhanniballake May 08 '20 at 03:42
  • Thanks for the reference. This is just a test POC to figure out how these navigation tags works, that's the reason I did not follow the guides you mention. The idea here is just to test a few things quickly. – MXC May 08 '20 at 13:29

1 Answers1

5

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>
Community
  • 1
  • 1
ianhanniballake
  • 191,609
  • 30
  • 470
  • 443