Android 导航组件 popUpTo 取回弹出的 fragment

Android Navigation component popUpTo taking back to popped up fragment

请不要将此标记为重复,我已经阅读了这些 SO 问题,但仍然无法正常工作 Android navigation component popUpTo behaviour

我正在使用

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

场景:这是我的应用图

这是我的导航代码:

<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>

当我在 MainFragmentLoginFragment 上按下后退按钮时,我仍然能够导航回 splashFragment。我已经有 app:popUpToapp:popUpToInclusive 标签。我不希望我的应用导航回 splashFragment

您在 popUpTo

中使用了错误的 ID

根据 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.

popUpTo 应该指向已经在返回堆栈上的目标,您要从返回堆栈中弹出。因此,如果您想弹出 splashFragment 的所有内容,您应该使用 app:popUpTo="@id/splashFragment"。如果你想弹出 splashFragment 的所有内容,那么你应该使用 app:popUpTo="@id/splashFragment"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>