Saving and restoring the state of the user interface of a fragment by pressing and pushing from the back of the screen

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.
+7
android android-fragments back-stack fragmentmanager savestate
source share

No one has answered this question yet.

See similar questions:

2
How to save and restore ExpandableListView state in Android?

or similar:

2510
How to keep Android activity state by saving instance state?
1111
Is abandonment of an application incredulous?
462
ViewPager and fragments - what is the right way to store fragment state?
455
Just for everyone, how to properly save the state of the fragment instance on the stack?
308
What is the difference between FragmentPagerAdapter and FragmentStatePagerAdapter?
261
Understanding the fragment of setRetainInstance (boolean)
60
How to cut a fragment
43
How to identify a fragment recovered from backstack
33
Android - saving / restoring fragment state
4
Saving the state of fragments in a stack

All Articles