手动将片段添加到导航架构组件的后台堆栈中

阿吉特·乔杜里

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

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

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

现在的问题是在上面的情况下,除了片段 D 之外,堆栈中不会有任何片段。但是我需要当用户按下后退按钮时,它应该导航到片段 C 到片段 B 到片段 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())

在 AFragment 中,我正在重定向到其他片段,假设为每个片段创建深层链接并不是一个好习惯。

此代码在onCreate()AFragment 方法中

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)。

尼蒂什

为此,请参阅处理导航组件下的自定义后退导航

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

替换后,片段未添加到后台堆栈

如何在Spring Boot中手动将数据添加到Redis并获取缓存的数据?

添加到后台堆栈后如何保持片段状态?

将SelectListItem手动添加到SelectList中以在DropDownListFor中使用

手动将行添加到StreamingHttpResponse(Django)

手动将Snapkit添加到xcode

禁用将片段添加到导航体系结构组件中的后堆栈

Rails 5-在更新时手动将属性添加到控制器中的参数

如何在Firestore数据库中手动将文档添加到集合?

Matplotlib:手动将更多颜色添加到axis.prop_cycle()中的默认颜色?

在Laravel 4.2中手动将行添加到雄辩的结果

使用SQL Server通过存储过程调用将记录手动添加到SQL选择查询的结果中

将手动p值添加到ggplot2中的分组条形图

手动添加线时将图例添加到ggplot

手动将书签添加到Microsoft Edge

手动将flexslider添加到Rails 4应用中-类型错误flexslider不起作用

将UINavigationController添加到导航堆栈

将UINavigationController添加到导航堆栈

是否需要手动删除手动添加到React组件中的元素的事件侦听器?

在Eclipse中手动将源添加到Maven项目中的依赖项

添加到在集合中的对象上手动添加属性的数组

将手动安装的应用程序添加到 Ubuntu 18.04 中的“显示应用程序”网格

有没有办法手动将“包”(例如 Python)添加到组中?

当当前片段事务未添加到后台堆栈中时,popBackStackImmediate 不显示片段

我可以手动将 dispatch 添加到组件的 props 而不是 connect() 吗?

如何手动将图例添加到 ggplot

为什么通过 JQuery 将项目添加到数组与手动添加不同?

尝试将 pvalues 手动添加到 ggplot R 中的箱线图后出错

将手动输入的数据添加到 javascript