使用导航组件在popUpTo的帮助下导航片段

时间:2019-08-22 13:08:43

标签: android kotlin android-jetpack android-jetpack-navigation

我有三个片段 Fragment1-> Fragment3-> Fragment4 ,因此,如果单击 Fragment4 中的后退按钮>我需要使用导航组件转到 Fragment1 ,我看到了popUpto,但我不理解。

你能告诉我我做错了吗?

我使用了popUpTo属性,但这不起作用

 <?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/first_fragment">

<fragment
    android:id="@+id/first_fragment"
    android:name="com.example.navbottom.ui.navScreen.fragments.FirstFragment"
    android:label="first_fragment"
    tools:layout="@layout/fragment_first">
    <action
        android:id="@+id/action_first_fragment_to_third_fragment"
        app:destination="@id/third_fragment" />
</fragment>

<fragment
    android:id="@+id/third_fragment"
    android:name="com.example.navbottom.ui.navScreen.fragments.ThirdFragment"
    android:label="Third_Fragment"
    tools:layout="@layout/fragment_third" >
    <action
        android:id="@+id/action_third_fragment_to_fourth_fragment"
        app:destination="@id/fourth_fragment" />
</fragment>

<fragment
    android:id="@+id/fourth_fragment"
    android:name="com.example.navbottom.ui.navScreen.fragments.FourthFragment"
    android:label="fragment_fourth"
    tools:layout="@layout/fragment_fourth" >
    <action
        android:id="@+id/action_fourth_fragment_to_first_fragment"
        app:destination="@id/first_fragment"
        app:popUpTo="@id/fourth_fragment"
        app:popUpToInclusive="false"/>
</fragment>
 </navigation>

没有任何反应 Fragment4-> Fragment3-> Fragment1

1 个答案:

答案 0 :(得分:0)

我正在解决相同的问题,我认为解决方案是

<action
  android:id="@+id/action_third_fragment_to_fourth_fragment"
  app:destination="@id/fourth_fragment"
  app:popUpTo="@id/first_fragment"
  app:popUpToInclusive="false"/>

导航到屏幕4时要删除后退堆栈(或弹出堆栈)。

对于3-> 4,您不需要包含在内。您的后排堆栈是:

1->导航至3

1-3->导航至4,将所有弹出 1

1-4->后退

1

<action
  android:id="@+id/action_fourth_fragment_to_first_fragment"
  app:destination="@id/first_fragment"
  app:popUpTo="@id/first_fragment"
  app:popUpToInclusive="true"/>

对于4-> 1,您需要删除“旧” 1,因此要包括popUpToInclusive

1->导航至3

1-3->导航至4,将所有弹出 1

1-4->导航至1

1-4-1 <-这就是为什么删除包括1在内的所有内容的原因。

这样,后退堆栈又变为“ 1”。

相关问题