对话框中的Android Jetpack导航组件结果 [英] Android jetpack navigation component result from dialog

查看:222
本文介绍了对话框中的Android Jetpack导航组件结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我仅使用导航组件就能够成功导航到对话框并返回.问题是,我必须在对话框中做一些事情,然后将结果返回到调用对话框的片段中.

So far I'm successfully able to navigate to dialogs and back using only navigation component. The problem is that, I have to do some stuff in dialog and return result to the fragment where dialog was called from.

一种方法是使用共享的viewmodel.但是为此,我必须使用.of(activity),即使我不再需要它,也使我的应用仅占用一个内存.

One way is to use shared viewmodel. But for that I have to use .of(activity) which leaves my app with a singleton taking up memory, even when I no longer need it.

另一种方法是重写show(fragmentManager,id)方法,获取对片段管理器的访问,并从中访问先前的片段,然后可以将其设置为targetfragment.在实现回调接口之前,我已经使用了targetFragment方法,因此我的对话框可以将结果通知给TargetFragment.但是在导航组件方法中,它感觉很笨拙,并且可能会在某一点或另一点停止工作.

Another way is to override show(fragmentManager, id) method, get access to fragment manager and from it, access to previous fragment, which could then be set as targetfragment. I've used targetFragment approach before where I would implement a callback interface, so my dialog could notify targetFragment about result. But in navigation component approach it feels hacky and might stop working at one point or another.

还有其他方法可以做我想要的吗?也许有一种方法可以解决第一种方法的问题?

Any other ways to do what I want? Maybe there's a way to fix issue on first approach?

推荐答案

在Navigation 2.3.0-alpha02及更高版本中,NavBackStackEntry允许访问SavedStateHandle. SavedStateHandle是可用于存储和检索数据的键值映射.这些值在进程终止(包括配置更改)之前一直保持不变,并通过同一对象保持可用.通过使用给定的SavedStateHandle,您可以在目标之间访问和传递数据.这是一种有用的机制,它是一种将数据从堆栈中弹出后从目的地取回数据的机制.

In Navigation 2.3.0-alpha02 and higher, NavBackStackEntry gives access to a SavedStateHandle. A SavedStateHandle is a key-value map that can be used to store and retrieve data. These values persist through process death, including configuration changes, and remain available through the same object. By using the given SavedStateHandle, you can access and pass data between destinations. This is especially useful as a mechanism to get data back from a destination after it is popped off the stack.

要将数据从目标B传递回目标A,首先将目标A设置为在其SavedStateHandle上侦听结果.为此,请使用getCurrentBackStackEntry()API检索NavBackStackEntry,然后观察SavedStateHandle提供的LiveData.

To pass data back to Destination A from Destination B, first set up Destination A to listen for a result on its SavedStateHandle. To do so, retrieve the NavBackStackEntry by using the getCurrentBackStackEntry() API and then observe the LiveData provided by SavedStateHandle.

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
val navController = findNavController();
// We use a String here, but any type that can be put in a Bundle is supported
navController.currentBackStackEntry?.savedStateHandle?.getLiveData("key")?.observe(
    viewLifecycleOwner) { result ->
    // Do something with the result.
}

}

在目标B中,必须使用getPreviousBackStackEntry()API在目标A的SavedStateHandle上设置结果.

In Destination B, you must set the result on the SavedStateHandle of Destination A by using the getPreviousBackStackEntry() API.

navController.previousBackStackEntry?.savedStateHandle?.set("key", result)

这篇关于对话框中的Android Jetpack导航组件结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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