Android Navigation - Is it possible to restore a bundle from upper fragment with popBackStack method?

拜拜、爱过 提交于 2021-01-04 14:47:16

问题


I was trying to pass data between fragments while I was passing from upper fragment to down fragment, but I don't want to use the navigate method. It adds a new destination to the process history, but I want back to the previous fragment, restore it data and refresh the view. Is it possible to do it with NavController and popBackStack method?


回答1:


After few hours I found a strange solution. findNavController().popBackStack() cannot pass data to the previous fragment, but we can do it using the findNavController().graph.defaultArguments, set the data in one fragment, and get the data in the second one. I believe someone will find the better solution.




回答2:


I also got same problem and I have used below solution which is working, But i am not sure its correct way or not.

val navOptions = NavOptions.Builder().setPopUpTo(R.id.id_of_fragment_one, true).build()
view?.findNavController()?.navigate(R.id.id_of_fragment_one, bundleOf("id" to model), navOptions)


来源:https://stackoverflow.com/questions/52687800/android-navigation-is-it-possible-to-restore-a-bundle-from-upper-fragment-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!