I am searching for postSticky()
method replacement. It is being used for simple passing value to previous Fragment, but thing is that I am using BackStackUtil for navigation so instance()
method is being called when returning only if stack gets cleared somehow before getting back.
Previous Fragment is holding List
of items, when next Fragment can modify picked item and yet another one can do something else so it is chain of sticky events when each of these is being passed to previous Fragment.
App structure won't let me to apply Coordinator pattern at current stage and also I don't want to attach Bundle to Fragments kept on stack.
I was looking for solutions but I couldn't find any. I also don't want to store values in some static fields or SharedPreferences/Data storage.
I was thinking about shared ViewModel but I don't really like this idea to be honest, so I would appreciate any ideas or just confirmation if shared VM is the only/best way.
Do you have any other ideas?
Asked
Active
Viewed 378 times
0

SkypeDogg
- 1,000
- 2
- 13
- 28
-
Just to understand the scenario. You are navigating to next fragment and then going back? If so, you can use the savedStateHandle on the back stack entry. Let me know if this is what you are looking for. – gioravered Sep 08 '21 at 06:30
-
I am navigating to next Fragment and then again to another one and after this I am going back to previous and again previous. So I have already 2 Fragments to take care of. But looks like `savedStateHandle` could be what I need. I have also found `setFragmentResultListener` and this would be nice alternative too, but appcompat version won't let me do this now, because it is available since 1.3.0 and I work on 1.2.0 and I can't change it right now. – SkypeDogg Sep 08 '21 at 07:19
-
Do you want me to help with savedStateHandle or you can do it alone? – gioravered Sep 08 '21 at 10:24
-
If you can, then yes. You can also put same answer as a post so I can accept it as answer to my question. Maybe it will help someone in future. – SkypeDogg Sep 09 '21 at 07:28
-
Check this first: https://stackoverflow.com/questions/50754523/how-to-get-a-result-from-fragment-using-navigation-architecture-component I think it covers the issue pretty well. – gioravered Sep 09 '21 at 07:57
-
This looks okay but I am not sure about one part of it "When the owner activity is finished, the framework calls the ViewModel objects's onCleared() method so that it can clean up resources.". App structure is not really created for this behaviour, because there's one host `Activity` with multiple `Fragment's`. Activity is being destroyed actually on app close, so all data stored in ViewModel is going to be there at all times if I get this solution right. – SkypeDogg Sep 09 '21 at 08:30
1 Answers
0
In your A fragment, before navigating to B fragment, listen to savedStateHandle:
findNavController()
.currentBackStackEntry
?.savedStateHandle?.getLiveData<Bundle>("DATA_KEY")
?.observe(viewLifecycleOwner) { result ->
// Result from fragment B
}
In your B fragment, before navigating back, set the data to pass to A fragment:
findNavController()
.previousBackStackEntry
?.savedStateHandle
?.set("DATA_KEY", result)
You can remove the observer using:
findNavController()
.currentBackStackEntry
?.savedStateHandle?.remove<Bundle>
Note that here the passed type is Bundle (the type in getLiveData<Bundle>) but you can use any type you want.

gioravered
- 1,758
- 3
- 19
- 30