Android 使用导航组件从Backstack中删除片段

Android 使用导航组件从Backstack中删除片段,android,android-jetpack,android-architecture-navigation,Android,Android Jetpack,Android Architecture Navigation,我相信有很多问题与这个问题类似。然而,我找不到解决我所面临问题的办法。问题是popBackStack函数会从backstack中删除片段。但是,当另一个新的导航发生时,它会崩溃。让我举个例子,我有三个片段,FragmentFirst,FragmentSecond,FragmentThird。一个导航图 <navigation xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app=

我相信有很多问题与这个问题类似。然而,我找不到解决我所面临问题的办法。问题是popBackStack函数会从backstack中删除片段。但是,当另一个新的导航发生时,它会崩溃。让我举个例子,我有三个片段,FragmentFirst,FragmentSecond,FragmentThird。一个导航图

<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_a"
    app:startDestination="@id/firstFragment">

    <fragment
        android:id="@+id/firstFragment"
        android:name="com.example.myapplication.FirstFragment"
        android:label="fragment_first"
        tools:layout="@layout/fragment_first" >
        <action
            android:id="@+id/action_firstFragment_to_secondFragment"
            app:destination="@id/secondFragment" />
    </fragment>
    <fragment
        android:id="@+id/secondFragment"
        android:name="com.example.myapplication.SecondFragment"
        android:label="SecondFragment" >
        <action
            android:id="@+id/action_secondFragment_to_thirdFragment"
            app:destination="@id/thirdFragment" />
    </fragment>
    <fragment
        android:id="@+id/thirdFragment"
        android:name="com.example.myapplication.ThirdFragment"
        android:label="ThirdFragment" >
    </fragment>
</navigation>
第二段

button2.setOnClickListener {
    view.findNavController().navigate(R.id.action_secondFragment_to_thirdFragment)
}
button3.setOnClickListener {
    findNavController().popBackStack(R.id.firstFragment, true)
}
第三部分

button2.setOnClickListener {
    view.findNavController().navigate(R.id.action_secondFragment_to_thirdFragment)
}
button3.setOnClickListener {
    findNavController().popBackStack(R.id.firstFragment, true)
}
现在,我可以从第一个导航到第二个,从第二个导航到第三个,从第三个导航到第一个。但当我再次按下第一个按钮时,它崩溃了,消息被删除了

id/action_firstFragment_to_secondFragment cannot be found from the current destination NavGraph
如果我在图中创建一个动作

<fragment
    android:id="@+id/thirdFragment"
    android:name="com.example.myapplication.ThirdFragment"
    android:label="ThirdFragment" >
    <action
        app:popUpTo="@id/firstFragment"
        app:popUpToInclusive="true"
        android:id="@+id/action_thirdFragment_to_firstFragment"
        app:destination="@id/firstFragment" />
</fragment>
在这种情况下,它可以完美地工作。但是,由于我正在使用的应用程序的限制,我想使用popBackStack


提前感谢。

popBackStack(R.id.firstFragment,true)
弹出第一个片段的所有内容。你已经把所有的碎片都从后面的堆栈中取出了。如果您只想返回到第一个片段,似乎您想使用
false
,而不是
true

您删除了第一个片段,然后您想导航到第一个片段,这怎么办@我在哪里删除了我的第一个片段?我只是在第三个片段中添加了这个动作,以显示它在哪里工作。但是我想做同样的事情,而不在第三个片段中声明该操作。
popBackStack(R.id.firstFragment,true)
弹出第一个片段的所有内容。你已经把所有的碎片都从后面的堆栈中取出了。如果你只想返回到第一个片段,似乎你想使用
false
,而不是
true
。@ianhanniballake你太棒了。是的,该值将为false。