android - 在导航架构组件中手动将 fragment 添加到后台堆栈中

标签 android uinavigationcontroller back-stack android-deep-link navigation-architecture

我在导航图中有 4 个 fragment ,名为 A、B、C、D,我从用户那里获取一些数据。

当我处于正常流程(A->B->C->D)并按后退按钮时,一切(后退堆栈)工作正常。

但是我有一个情况,我有并编辑信息流,假设用户将表单留在 fragment D 上,所以当用户再次出现在屏幕上时,我必须在同一屏幕上导航用户,即 fragment D。 我通过深层链接实现了它。

现在的问题是在上述情况下,除了 fragment D 之外,堆栈中不会有任何 fragment 。但是我需要当用户按下后退按钮时,它应该导航到 fragment C 到 fragment B 到 fragment A .

意味着我想要其他 fragment (A,B,C)添加到后堆栈中。我们可以做同样的事情吗?如果可以,怎么做?

===这是代码===

my_navigation_graph.xml。

<fragment
    android:id="@+id/aFragment"
    android:name="in.demo.project.AFragment"
    android:label="AFragment"
    tools:layout="@layout/fragment_a_layout">
    <deepLink
        android:id="@+id/deepLink"
        android:autoVerify="true"
        app:uri="demo://editflow?is_for_edit={is_for_edit}&amp;step={step}" />
    <argument
        android:name="is_for_edit"
        android:defaultValue="false"
        app:argType="boolean" />
    <argument
        android:name="step"
        android:defaultValue="2"
        app:argType="string" />

    <action
        android:id="@+id/action_aFragment_to_bFragment"
        app:destination="@id/bFragment"/>

    <action
        android:id="@+id/action_aFragment_to_cFragment"
        app:destination="@id/cFragment"/>
    <action
        android:id="@+id/action_aFragment_to_dFragment"
        app:destination="@id/dFragment"/>


<fragment
    android:id="@+id/bFragment"
    android:name="in.demo.project.BFragment"
    android:label="BFragment"
    tools:layout="@layout/fragment_b_layout">
    <action
        android:id="@+id/action_bFragment_to_cFragment"
        app:destination="@id/cFragment" />
</fragment>

<fragment
    android:id="@+id/cFragment"
    android:name="in.demo.project.CFragment"
    android:label="CFragment"
    tools:layout="@layout/fragment_c_layout">

    <action
        android:id="@+id/action_cFragment_to_dFragment"
        app:destination="@id/dFragment" />

</fragment>
<fragment
    android:id="@+id/dFragment"
    android:name="in.demo.project.DFragment"
    android:label="DFragment"
    tools:layout="@layout/fragment_d_layout">
    
</fragment>

浏览。

findNavController().navigate(AFragmentDirections.actionAFragmentToBFragment())

Inside AFragment i am redirecting to other fragment, assuming that creating deep-link to every fragment is not good practice.

这段代码位于AFragment的onCreate()方法中

arguments?.let {
            if (AFragmentArgs.fromBundle(it).isForEdit) {
                redirectionForEdit(AFragmentArgs.fromBundle(it).step)
            } 
        }

和一个重定向函数

  private fun redirectionForEdit(step: String) {
        when (step) {
            "2" ->
                findNavController().navigate(AFragmentDirections.actionAFragmentToBFragment())
            "3" ->
                findNavController().navigate(AFragmentDirections.actionAFragmentToCFragment())
            "4" ->
                findNavController().navigate(AFragmentDirections.actionAFragmentToDFragment())  

        }

    }

现在,如果我在该步骤中得到 4,它将重定向到 DFragment,因此堆栈将是 A->D,但我也希望 B 和 D 进入堆栈(A->B->C->D)。

最佳答案

为此请参阅handling custom back navigation under navigation components

OnBackPressedCallback callback = new OnBackPressedCallback(true /* enabled by default */) {
        @Override
        public void handleOnBackPressed() {
            // Handle the back button event
        }
    };
    requireActivity().getOnBackPressedDispatcher().addCallback(this, callback);

关于android - 在导航架构组件中手动将 fragment 添加到后台堆栈中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66473159/

相关文章:

android - 如何从 FragmentManager 中删除之前的返回堆栈条目?

android - 了解Android中的返回堆栈概念

android - Google 云消息传递示例

android - 谷歌地图 getMap 返回 null

ios - 在 UINavigationController 中的 UIViewController 之间创建父/子关系?

ios - Swift:SWReavealController 无法滑动以关闭菜单 "only open with swipe"

android - noHistory 与 finish() - 哪个是首选?

android - Robolectric 缓慢并发出警告

android - 截断的 Logcat 异常消息

ios - 如何以编程方式从 iOS Swift 3 中的 XIB 推送并呈现给 UIViewController