清除Android导航组件中的后退堆栈



当显示主片段时,我正试图从后堆栈中清除所有片段。在导航图中,我添加了这个动作

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

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

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

我从这里的文档中了解到https://developer.android.com/guide/navigation/navigation-navigate则CCD_ 3清除所有堆栈直到目的地。但当我反击时,什么都没有清除。

更新:我已将此操作更改为popUpTo=登录。它只删除了登录片段,而没有删除之前的2个片段

<action
android:id="@+id/action_loginFragment_to_homeFragment"
app:destination="@id/homeFragment"
app:popUpTo="@+id/loginFragment"
app:popUpToInclusive="true" />```

我清除后台包中所有片段的方法是在popUpTo值中添加图id

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

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

最新更新