I'm trying to convert an Acticity + Fragments app to Flow + Mortar + Dagger2
I would like to save & restore screen state when jumping from screen to screen (at least backwards). What is the prefered/recommanded way to do that ?
I have spent quite a lot of time looking at flow and mortar readmes and samples but couldn't figure it out (the documentation and samples are minimal and only deal with simple/static/unique data).
Say, for example, you have a browser-like app that moves from Page to Page Where each Page use the same same PageView class, the same PagePresenter Class but have different dynamic content depending on a url-string typed by a user
It's quite complex/hard to use Dagger2 (compile type annotation) to save/restore the states by injection, right ? (This would require a complex parent/cache structure)
I googled a bit and stumbled upon : https://github.com/lukaspili/flow-navigation
Yet this is mostly an experiment... If possible, I would rather base my production money making app on a solution that is official/reliable/tested/supported/backed by square
I also looked at :
1) https://github.com/square/flow/issues/11 But the sample injects data with Dagger in 2 screens with different view classes (not a valid answer)
2) https://github.com/square/flow/issues/76 (no answer given)
3) Mortar / Flow save view and presenter in backstack
I saw this also : We're doing this now internally by simply adding a mutable field to our screen objects:
public void setViewState(SparseArray<Parcelable> viewState) {
this.viewState = viewState;
}
public void restoreHierarchyState(View view) {
view.restoreHierarchyState(viewState);
}
When a View is swapped out, we grab its instance state and set it on the screen object (which is already on the backstack). We're going to live with this pattern a little while before promoting it into the library.
But neither the flow sample nor the mortar sample use this solution (they use dagger2 to inject lists...not a valid solution again)
So, what is the UP TO DATE best/recommanded way to restore/save screen state in a mortar+flow (+dagger2) app ?
After experimenting a bit, I added some mutable fields to my Path objects.
By design, a mortar/Flow app uses a StateParceler to serialize/unserialize those Path objects to/from Bundles and Parcels in order to save and restore view states
By making the StateParceler take care of those mutable fields, they are able to survive orientation change and back-navigation.
By making the StateParceler also able to serialize/unserialize those mutable fields to Persistant storage (say JSon and SharedPreferences), a complete history can survive power down/different app sessions
The sample from square uses a GsonParceler that is able to do that out of the box for most objects. You just have to write some code to make it able to take care of collections and complex objects with Generics/interface...