我发现了关于在android 文档上使用导航库返回数据
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<String>
("key")?.observe(
viewLifecycleOwner) { result ->
// Do something with the result.
}
}
和
navController.previousBackStackEntry?.savedStateHandle?.set("key", result)
但是,如果我使用全局操作执行以下操作,我无法使其工作:片段 x ->(全局操作)片段 A ->片段 B ->(popbackstack(A,true))->片段 X
这是有意的还是有其他方法可以解决这个问题?(我知道 viewModels,但只是想尝试新技术)
编辑:从文档添加代码