I just searched the docs about using and implement the new Navigation Component, and I dont see or find a method to send data to previous fragment.
Asked
Active
Viewed 1,466 times
2
-
1Does this answer your question? [Equivalent of startActivityForResult() with Android Architecture Navigation](https://stackoverflow.com/questions/50702643/equivalent-of-startactivityforresult-with-android-architecture-navigation) – Nimrod Dayan Feb 23 '20 at 19:17
-
thanks for the tip, I am already observing that "bug/feature" on Google BugReport and the solution they provide, is still in Alpha, hope they release to production fast – Catluc Feb 24 '20 at 06:52
1 Answers
0
There is a feature request for this feature, you can star it: https://issuetracker.google.com/issues/79672220.
But for now you can implement this using LiveData and ViewModel, First fragment will observe some LiveData object inside your ViewModel, second fragment will change this object value, and when you will go back to first fragment it will be notified that the value is changed.
Alex
- 9,102
- 3
- 31
- 35
-
Correct me, but i think u messed up things. 1st Fragment and 2nd Fragment need to have some Viewodel shared, but on the same activityScope, and not in neither of Fragments, because the viewModel of Fragment will be destroyed when the fragment is destroyed. https://developer.android.com/topic/libraries/architecture/viewmodel#lifecycle This leads to have a lot of trash in the activity in a large app, with single activity – Catluc Nov 21 '18 at 11:01
-
Yes you will need to have viewmodel with activity scope, so its data can be shared between fragments. How having viewmodel with activity scope leads to a lot of trash in activity? – Alex Nov 21 '18 at 11:33
-
If i did understand correct, Androidx way of doing things is to have one activity (NavHost), and multiple fragments inside it. So imagine a lot of fragments sharing data beetwen one activity viewModel. That activity viewModel will be very caotic and it will end up as a container. I understand that is possible for the activity to have more than 1 viewmodel associatted, and this will clean things up, but it still dont taste well – Catluc Nov 22 '18 at 10:41
-
There is no problem using multiple viewmodels with activity scope, and it is very clean and good solution. And for now i think it is the best solution to implement "onActivityResult" for destinations in navigation architecture component. – Alex Nov 22 '18 at 12:29