在我的应用程序中,我使用了一个活动和四个片段。在这个应用程序中,我使用了导航组件。 我的片段加载顺序是A-> B-> C-> D
在片段D中,在设备后退按钮上按下,我想从后堆栈中移除片段C,以便可以打开片段B。 有人建议我该怎么办 预先谢谢你
这是我的navigation_graph.xml
<?xml version="1.0" encoding="utf-8"?>
<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/navigation_graph"
app:startDestination="@id/firstFragment"
>
<fragment
android:id="@+id/firstFragment"
android:name="com.education.javaprogramming.fragments.MainFragment"
android:label="fragment_first"
tools:layout="@layout/activity_main" >
<action
android:id="@+id/action_firstFragment_to_displayList1Fragment"
app:destination="@id/displayList1Fragment"
app:enterAnim="@anim/enter"
app:exitAnim="@anim/exit"
app:popEnterAnim="@anim/pop_enter"
app:popExitAnim="@anim/pop_exit"
/>
<action
android:id="@+id/action_firstFragment_to_displayList2Fragment"
app:destination="@id/displayList2Fragment"
app:enterAnim="@anim/enter"
app:exitAnim="@anim/exit"
app:popEnterAnim="@anim/pop_enter"
app:popExitAnim="@anim/pop_exit"
/>
<action
android:id="@+id/action_firstFragment_to_helpFragment"
app:destination="@id/helpFragment"
app:enterAnim="@anim/enter"
app:exitAnim="@anim/exit"
app:popEnterAnim="@anim/pop_enter"
app:popExitAnim="@anim/pop_exit"
/>
<action
android:id="@+id/action_firstFragment_to_quizzFragment"
app:destination="@id/quizzFragment"
app:enterAnim="@anim/enter"
app:exitAnim="@anim/exit"
app:popEnterAnim="@anim/pop_enter"
app:popExitAnim="@anim/pop_exit"
/>
</fragment>
<fragment
android:id="@+id/displayList1Fragment"
android:name="com.education.javaprogramming.fragments.DisplayList1Fragment"
android:label="fragment_display_list1"
tools:layout="@layout/fragment_display_list1"
>
<argument
android:name="position1"
app:argType="integer" />
<action
android:id="@+id/action_displayList1Fragment_to_firstFragment"
app:destination="@id/firstFragment" />
<argument
android:name="name"
app:argType="string" />
<action
android:id="@+id/action_displayList1Fragment_to_displayList2Fragment"
app:destination="@id/displayList2Fragment"
app:enterAnim="@anim/enter"
app:exitAnim="@anim/exit"
app:popEnterAnim="@anim/pop_enter"
app:popExitAnim="@anim/pop_exit"
/>
</fragment>
<fragment
android:id="@+id/displayList2Fragment"
android:name="com.education.javaprogramming.fragments.DisplayList2Fragment"
android:label="fragment_display_list2"
tools:layout="@layout/fragment_display_list2" >
<action
android:id="@+id/action_displayList2Fragment_to_displayList1Fragment"
app:destination="@id/displayList1Fragment"
app:enterAnim="@anim/enter"
app:exitAnim="@anim/exit"
app:popEnterAnim="@anim/pop_enter"
app:popExitAnim="@anim/pop_exit"
/>
<action
android:id="@+id/action_displayList2Fragment_to_firstFragment"
app:destination="@id/firstFragment"
app:enterAnim="@anim/enter"
app:exitAnim="@anim/exit"
app:popEnterAnim="@anim/pop_enter"
app:popExitAnim="@anim/pop_exit"
/>
<argument
android:name="position1"
app:argType="integer" />
<argument
android:name="position2"
app:argType="integer" />
<argument
android:name="name"
app:argType="string" />
<action
android:id="@+id/action_displayList2Fragment_to_displayListInDeFragment"
app:destination="@id/displayListInDeFragment"
app:enterAnim="@anim/enter"
app:exitAnim="@anim/exit"
app:popEnterAnim="@anim/pop_enter"
app:popExitAnim="@anim/pop_exit"
/>
</fragment>
<fragment
android:id="@+id/displayListInDeFragment"
android:name="com.education.javaprogramming.fragments.DisplayListInDeFragment"
android:label="fragment_display_list_in_de"
tools:layout="@layout/fragment_display_list_in_de" >
<action
android:id="@+id/action_displayListInDeFragment_to_displayList2Fragment"
app:destination="@id/displayList2Fragment"
app:enterAnim="@anim/enter"
app:exitAnim="@anim/exit"
app:popEnterAnim="@anim/pop_enter"
app:popExitAnim="@anim/pop_exit"
/>
<argument
android:name="position1"
app:argType="integer" />
<argument
android:name="position2"
app:argType="integer" />
<argument
android:name="position3"
app:argType="integer" />
<argument
android:name="name"
app:argType="string" />
</fragment>
<fragment
android:id="@+id/helpFragment"
android:name="com.education.javaprogramming.fragments.HelpFragment"
android:label="fragment_help"
tools:layout="@layout/fragment_help" >
<action
android:id="@+id/action_helpFragment_to_firstFragment"
app:destination="@id/firstFragment"
app:enterAnim="@anim/enter"
app:exitAnim="@anim/exit"
app:popEnterAnim="@anim/pop_enter"
app:popExitAnim="@anim/pop_exit"
/>
<argument
android:name="position1"
app:argType="integer" />
<argument
android:name="name"
app:argType="string" />
<action
android:id="@+id/action_helpFragment_to_aboutFragment"
app:destination="@id/aboutFragment"
app:enterAnim="@anim/enter"
app:exitAnim="@anim/exit"
app:popEnterAnim="@anim/pop_enter"
app:popExitAnim="@anim/pop_exit"
/>
</fragment>
<fragment
android:id="@+id/aboutFragment"
android:name="com.education.javaprogramming.fragments.AboutFragment"
android:label="fragment_about"
tools:layout="@layout/fragment_about" >
<action
android:id="@+id/action_aboutFragment_to_helpFragment"
app:destination="@id/helpFragment"
app:enterAnim="@anim/enter"
app:exitAnim="@anim/exit"
app:popEnterAnim="@anim/pop_enter"
app:popExitAnim="@anim/pop_exit"
/>
</fragment>
<fragment
android:id="@+id/quizzFragment"
android:name="com.education.javaprogramming.fragments.QuizzFragment"
android:label="fragment_quizz"
tools:layout="@layout/fragment_quizz" >
<action
android:id="@+id/action_quizzFragment_to_firstFragment"
app:destination="@id/firstFragment"
app:enterAnim="@anim/enter"
app:exitAnim="@anim/exit"
app:popEnterAnim="@anim/pop_enter"
app:popExitAnim="@anim/pop_exit"
/>
<action
android:id="@+id/action_quizzFragment_to_displayExamFragment"
app:destination="@id/displayExamFragment" />
</fragment>
<fragment
android:id="@+id/displayExamFragment"
android:name="com.education.javaprogramming.fragments.DisplayExamFragment"
android:label="fragment_display_exam"
tools:layout="@layout/fragment_display_exam" >
<action
android:id="@+id/action_displayExamFragment_to_quizzFragment"
app:destination="@id/quizzFragment"
app:enterAnim="@anim/enter"
app:exitAnim="@anim/exit"
app:popEnterAnim="@anim/pop_enter"
app:popExitAnim="@anim/pop_exit"
/>
<argument
android:name="position1"
app:argType="integer" />
<action
android:id="@+id/action_displayExamFragment_to_resultFragment"
app:destination="@id/resultFragment" />
</fragment>
<fragment
android:id="@+id/resultFragment"
android:name="com.education.javaprogramming.fragments.ResultFragment"
android:label="fragment_result"
tools:layout="@layout/fragment_result" >
<action
android:id="@+id/action_resultFragment_to_quizzFragment"
app:destination="@id/quizzFragment" />
<argument android:name="givenAnswers"
app:argType="reference"
/>
<argument android:name="actualAnswers"
app:argType="reference"
/>
</fragment>
</navigation>
答案 0 :(得分:2)
您应该使用
app:popUpTo="@id/id_of_c_fragment"
app:popUpToInclusive="true"
app:popUpTo="@id/id_of_d_fragment"
app:popUpToInclusive="true"
在操作范围内的导航图中。
答案 1 :(得分:0)
您可以使用navController.popBackStack(int destinationId,bool inclusive)方法,(请注意boolean
的值)
尝试将控制器的后退栈弹回到特定的目的地。例如。
navController.popBackStack(R.id.dest_id_of_B, true)
答案 2 :(得分:0)
在片段D中,如果要返回片段B(删除片段C),则可以使用:
Navigation.findNavController(requireView()).popBackStack(
R.id.id_of_fragment_C, true)
id_of_fragment_C
是您要跳过的片段的ID。