Background:
I have the main Activity , it wraps the main Fragment , which can be changed, and in order to save the backstack, I use the FragmentManager backstack.
The main difference from storing the action stack is that when a fragment is popped into the backstack and replaced, it will call onDestroyView() , but not onDestroy() , and when it returns, the view will be re-created with onCreateView() . (however, onCreate() not called because the fragment object is not located)
On the action stack, this will not happen, and the views remain.
This has a positive effect on lower-end devices, since Android can free some memory, and you donβt need to maintain the correct views (in my application messages from the server, the view can change at any time), so one can also save precious bandwidth.
Actual problem:
Say I have a fragment, and the user clicks on something, and its appearance changes, for example. the list is expanded.
If the user switches to another screen (i.e., fragment), the previous fragment will be pushed into the backstack, and its viewing will be destroyed.
When the user returns, the fragment will be recreated and will not βrememberβ the changes made by the user, for example. the list will not expand as it should
so how can I save the state and restore it without doing any special cases for each species?
Unwanted responses:
- keep the view alive: to do something to keep the view would violate the effectiveness of the fragment
- using
onSaveInstanceState() : it will not be called when the fragment is popped into the backstack, because the action will not be destroyed and it will not be changed. - special object: prefer not to do this if there is a way the system can do this for you.
android android-fragments back-stack fragmentmanager savestate
ndori
source share