如何在不返回Fragment C的情况下直接从Fragment D返回到Fragment A?

3

我使用Android Navigation Architecture Component。

我有三个Fragment:A、B和C

  • Fragment A -> Fragment B -> Fragment D
  • Fragment A -> Fragment C -> Fragment D

在Fragment D中,我有一个按钮用于验证修改并完成操作。从这个Fragment D开始,我想:

  1. 当我从Fragment B来时返回到Fragment B(我使用findNavController().navigateUp()
  2. 当我从Fragment C来时返回到Fragment A(我不知道如何做)

我怎样能够直接从Fragment D返回到Fragment A而不必经过Fragment C呢?

我应该等待Fragment C的返回,并重新处理findNavController().NavigateUp()的执行吗?


请查看此链接:https://dev59.com/j2Up5IYBdhLWcg3w5Kk6,您可以通过名称弹出堆栈。 - zgc7009
谢谢,但这迫使我在我的 Fragment D 中创建一个条件吗?如果前一个片段是 C,则转到 A,否则如果前一个片段是 B,则转到 B? - Dev Loots
如果你想要像那样导航,为什么不直接使用片段事务呢? - A_Singh7
@A_Singh7 的评论更好,我只是在手机上匆匆一瞥了一下评论。肯定要使用 Fragment Transaction 并以此方式管理您的堆栈。 - zgc7009
1
我通过在D片段中使用以下代码来实现这一点:if (isComeFromC == true) { findNavController().popBackStack(R.id.FragmentC, false) } findNavController().navigateUp() - Dev Loots
@Dev Loots 正试图避免那种情况,至少根据他对我最初评论的回应来看。 - zgc7009
3个回答

1

1
你可以通过findNavController().NavigateUp()来实现这两个操作。
例如,给定以下图形: graph
   <?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/test_nav_graph"
        app:startDestination="@id/a">

        <fragment
            android:id="@+id/a"
            android:name="fragment.A"
            tools:layout="@layout/a_fragment"
            android:label="A" >
            <action
                android:id="@+id/action_a_to_b"
                app:destination="@id/b" />
            <action
                android:id="@+id/action_a_to_c"
                app:destination="@id/c" />
        </fragment>
        <fragment
            android:id="@+id/b"
            android:name="fragment.B"
            tools:layout="@layout/b_fragment"
            android:label="B" >
            <action
                android:id="@+id/action_b_to_d"
                app:destination="@id/d" />
        </fragment>
        <fragment
            android:id="@+id/c"
            tools:layout="@layout/c_fragment"
            android:name="fragment.C"
            android:label="C" >
            <action
                android:id="@+id/action_c_to_d"
                app:destination="@id/d"
                app:popUpTo="@id/a"/>
        </fragment>
        <fragment
            android:id="@+id/d"
            tools:layout="@layout/d_fragment"
            android:name="fragment.D"
            android:label="D" />
    </navigation>

当您选择路径:A->B->D时,您的后退栈是A、B、D,当您调用NavigateUp()时,您会回到B。
现在,路径A->C->D在action标签中有一个小的添加,如您所见。
    <action
    android:id="@+id/action_c_to_d"
    app:destination="@id/d"
    app:popUpTo="@id/a"/>

它有popUpTo,所以当你从C导航到D时,app:popUpTo告诉导航库在调用navigate()时弹出一些目的地,因此导航到D后,你的返回堆栈是A、D,NavigateUp()带你回到A


1
当您在D片段中并按下返回按钮时,您可以检查片段堆栈并检查示例。
onBackPress(){
childFragmentManager.popbackstack()
val currentFragment = findFragmentByTag("FRAGMENT_B")
if(currentFragment == childFragmentManager.primaryNavigation()){
childFragmentManager.popbackstack()
}

注意

以上代码片段是不使用编辑器编写的。您可以使用此逻辑。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接