Android 导航组件中的清除返回堆栈

Doh*_*oha 3 android android-jetpack-navigation

当显示主片段时,我试图清除返回堆栈中的所有片段。在导航图中我添加了这个操作

<fragment
       android:id="@+id/loginFragment"
       android:name="com.test.navTest.ui.fragment.LoginFragment"
       android:label="fragment_login"
       tools:layout="@layout/login_fragment"
       >
       <action
           android:id="@+id/action_loginFragment_to_homeFragment"
           app:destination="@id/homeFragment"
           app:popUpTo="@+id/homeFragment"

           />
 </fragment>
Run Code Online (Sandbox Code Playgroud)

并在登录片段中。我试过 findNavController().navigate(LoginFragmentDirections.actionLoginFragmentToHomeFragment())

findNavController().navigate(R.id.action_loginFragment_to_homeFragment)

据我从此处的文档了解,https://developer.android.com/guide/navigation/navigation-navigatepopUpTo清除所有堆栈,直至到达目的地。但当我按回时,什么也没有清除。

更新:我将此操作更改为 popUpTo=Login。它仅删除了登录片段,而不删除了之前的 2 个片段。我应该在登录之前向片段的操作添加任何参数吗

<action
            android:id="@+id/action_loginFragment_to_homeFragment"
            app:destination="@id/homeFragment"
            app:popUpTo="@+id/loginFragment"
            app:popUpToInclusive="true" />```
Run Code Online (Sandbox Code Playgroud)

Doh*_*oha 7

对我来说清除后台堆栈中的所有片段的方法是将图形 id 添加到值popUpTo

<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/nav_graph"
    app:startDestination="@id/splashFragment">
     <fragment
        android:id="@+id/loginFragment"
        android:name="com.test.navTest.ui.fragment.LoginFragment"
        android:label="fragment_login"
        tools:layout="@layout/login_fragment">
        <action
            android:id="@+id/action_loginFragment_to_homeFragment"
            app:destination="@id/homeFragment"
            app:popUpTo="@+id/nav_graph"
            app:popUpToInclusive="true" />
    
    </fragment>
Run Code Online (Sandbox Code Playgroud)

当我添加popUpTo = @+id/login_fragment. 它仅删除了登录片段。