【问题标题】:How can I go back directly to Fragment A since Fragment D without going back to Fragment C?我怎样才能从 Fragment D 直接返回 Fragment A 而无需返回 Fragment C?
【发布时间】:2020-04-17 13:17:47
【问题描述】:

我使用 Android 导航架构组件。

我有三个片段 A、B 和 C

  • 片段 A -> 片段 B -> 片段 D
  • 片段 A -> 片段 C -> 片段 D

在片段 D 上,我有一个用于验证修改和完成的按钮。 从这个片段 D 我想:

  1. 当我从 Fragment B 来时返回 Fragment B(我用findNavController().navigateUp()
  2. 当我从Fragment C来的时候回到Fragment A(我不知道该怎么做)

如果我从 Fragment D 来而不返回 Fragment C,如何直接返回 Fragment A?

我是否应该等待 Fragment C 的返回并重新处理以执行 findNavController().NavigateUp()

【问题讨论】:

  • 勾选这个,stackoverflow.com/questions/14971780/…你可以按名字弹出栈。
  • 谢谢,但这迫使我在我的片段 D 中创建一个条件?如果前一个片段是C,那么去A,否则如果前一个片段是B,那么去B?
  • 如果你想这样导航,那你为什么不简单地使用片段事务呢?
  • @A_Singh7 更好的评论,我只是看了一眼手机上的评论。请务必使用 Fragment Transaction 并以这种方式管理您的堆栈。
  • 我通过在片段 D 中使用此代码来实现这一点:if (isComeFromC == true) { findNavController().popBackStack(R.id.FragmentC, false) } findNavController().navigateUp()

标签: android navigation fragment android-navigation


【解决方案1】:

我在 NavOptions 上使用 setPopUpTo,或者你也可以在 xml 中使用它

文档 => https://developer.android.com/reference/androidx/navigation/NavOptions.Builder#setPopUpTo(int,%20boolean)

【讨论】:

    【解决方案2】:

    您可以使用findNavController().NavigateUp() 实现这两个操作

    以下图为例:

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

    【讨论】:

      【解决方案3】:

      所以当你在 Fragment D 并按下返回按钮时。您可以检查片段堆栈并检查例如

      onBackPress(){
      childFragmentManager.popbackstack()
      val currentFragment = findFragmentByTag("FRAGMENT_B")
      if(currentFragment == childFragmentManager.primaryNavigation()){
      childFragmentManager.popbackstack()
      }
      

      注意

      上面的 sn-p 是在没有编辑器的情况下编写的。你可以使用这个逻辑。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-08-03
        • 2014-04-24
        • 1970-01-01
        • 2018-11-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多