组件导航,带参数从后台弹出 [英] Component Navigation , pop from backstack with arguments

查看:16
本文介绍了组件导航,带参数从后台弹出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有三个片段,A、B、C;

A -> B <-> C

B 和 C 之间是循环关系.B 或 C 片段都需要 参数,示例

 val args = Bundle()args.putString("StringKeyBC", argValueBtoC)findNavController().navigate(R.id.action_fragmentB_to_fragmentC, args, null)args.putString("StringKeyCB", argValueCtoB)findNavController().navigate(R.id.action_fragmentC_to_fragmentB, args, null)

问题是每次我在 B &C,片段被添加到后台堆栈,我不想要那个.如果片段已经返回堆栈,我只想弹出它,但如果我使用 popBackStack 我不能再添加参数:

public boolean popBackStack(@IdRes int destinationId, boolean inclusive)

那么,我怎样才能在两个片段之间不断切换而不每次都将它们添加到后台堆栈?

解决方案

您可以简单地通过将 popUpTo 属性添加到导航操作来从返回堆栈中弹出片段.通过这种方式,您可以使用带参数的操作进行导航,但具有弹回堆栈行为.

例如,您可以将属性 app:popUpTo="@+id/fragmentB" 添加到操作 action_fragmentC_to_fragmentB.这样,每次从 FragmentC 转到 FragmentB 时,您都会从 backstack 中弹出 FragmentC.

在此处查看带有示例的文档.p>

还有另一个选项,这可能是您描述的情况的开销,但允许使用 popBackStack 方法并发送参数 - 使用导航返回结果"方法.对于它,片段应该使用接收包的方法实现接口(回调).在调用 popBackStack 后,使用片段管理器中的 addOnBackStackChangedListener 触发此方法,提供所有必要的数据.(在如何返回结果?"部分中进行了描述:https://medium.com/google-developer-experts/using-navigation-architecture-component-in-a-large-banking-app-ac84936a42c2,并且这里的实现略有不同:https://medium.com/@zawadz88/david-vávra-thank-you-for-this-great-article-ae3e602b880a)

Let's say I have three fragments, A, B, C;

A -> B <-> C

Between B and C it is a circular relationship. Either B or C fragments requires arguments, example

     val args = Bundle()

     args.putString("StringKeyBC", argValueBtoC)       
     findNavController().navigate(R.id.action_fragmentB_to_fragmentC, args, null)

     args.putString("StringKeyCB", argValueCtoB)       
     findNavController().navigate(R.id.action_fragmentC_to_fragmentB, args, null)

The problem is that every time I move between B & C, the fragments are added to back stack and I don't want that. If the fragment is already to back stack I want just to pop it, but if I use popBackStack I can not add arguments anymore:

public boolean popBackStack(@IdRes int destinationId, boolean inclusive)

So, how can I constanlty switch between the two fragments without adding them every time to back stack?

解决方案

You can pop fragments from back stack simply by adding a popUpTo attribute to a navigation action. This way you navigate using an action with arguments, but with pop back stack behaviour.

For example, you can add attribute app:popUpTo="@+id/fragmentB" to the action action_fragmentC_to_fragmentB. This way you'll be popping fragmentC from backstack each time you go from fragmentC to fragmentB.

See the docs with example for this here.

There's another option, which is likely an overhead for the case you described, but that allows to use popBackStack method and send arguments - using 'navigate back with result' approach. For it fragments should implement an interface (callback) with a method that receives bundle. Use addOnBackStackChangedListener in the fragment manager to trigger this method, providing all the data necessary, after popBackStack is called. (Described here in the section "How to navigate back with a result?": https://medium.com/google-developer-experts/using-navigation-architecture-component-in-a-large-banking-app-ac84936a42c2, and with slightly different implementation here: https://medium.com/@zawadz88/david-vávra-thank-you-for-this-great-article-ae3e602b880a)

这篇关于组件导航,带参数从后台弹出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆