Android 刚刚为此发布了解决方案;在 Destinations ( Navigation 2.3.0-alpha02 ) 之间传递数据,基本上,在片段 A 中,您观察变量的变化,在片段 B 中,您在执行之前更改该值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<String>("key")?.observe(
viewLifecycleOwner) { result ->
// Do something with the result.
}
}
片段 B:
navController.previousBackStackEntry?.savedStateHandle?.set("key", result)
navController.popBackStack()