Pass data back to previous fragment using Android Navigation

元气小坏坏 提交于 2019-12-10 13:35:17

问题


I've started using Android Architecture Components (Navigation and Safe Args, View Models) along with Koin library.

Currently, I've got a problem with passing arguments between two fragments - I need to pass a string value from fragment A to fragment B, modify this value in fragment B and pass it back to fragment A.

I've found one possible solution to my problem - shared view models. Unfortunately, this approach has one problem because I can pass and modify values between screens, but when the fragment A navigate to another destination the value in the shared view model is still stored and not cleared.

Is there any different solution of passing and modifying data between fragments in Android Navigation? I want to avoid clearing this one value by hand (when the fragment A is destroyed).


回答1:


1) Pass string from Fragment A to Fragment B with action_A_to_B and SafeArgs.

2) popBackStack to remove Fragment B.

navController.popBackStack(R.id.AFragment, false);

or

navController.popBackStack();

3) Then pass modified data from B to A with action_B_to_A.

EDIT.

Here you have some another solution



来源:https://stackoverflow.com/questions/56243119/pass-data-back-to-previous-fragment-using-android-navigation

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