更新时间:2023-01-10 11:36:30
Android刚刚为此发布了一个解决方案. 在目标之间传递数据(导航2.3.0-alpha02 ),基本上,在片段A中您会观察到变量的变化,而在片段B中您会在执行popBackStack()
之前更改该值.
Android just released a solution for this; Passing data between Destinations (Navigation 2.3.0-alpha02), basically, in fragment A you observe changes in a variable and in fragment B you change that value before executing popBackStack()
.
片段A:
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:
navController.previousBackStackEntry?.savedStateHandle?.set("key", result)
navController.popBackStack()