Save the page state, which can be returned to the previous state in case of abnormal page exit reconstruction or configuration changes such as screen rotation, so that the user experience is better.

This article distinguishes between the granularity of saved objects: onSaveInstanceState() and ViewMadel

Local storage: a persistent storage solution. Usually used more, and can be used with the above two. Both of these are temporary storage solutions

OnSaveInstanceState () is saved on disk and is suitable for small objects. Viewmodels are held in memory and are suitable for holding complex objects

onSaveInstanceState()

Suitable for page reconstruction due to memory shortage or page reconstruction after configuration change, save a small amount of small object data

Two cases

  1. onSaveInstanceState()
  2. OnSaveInstanceState fragments. SetRetainInstance (true)
onSaveInstanceState()

Is called periodically. It retains data about the UI in two cases:

  1. Applications stop when running in the background due to memory limitations.
  2. Configuration changes

Called after activity.onstop and before Finish. However, this is not invoked when the user calls Finish or actively closes the activity

It was an error to assume that the call will only be made in the exception state. Instead, it is called whenever an exception state or activity enters the background. After Android P, after onStop; Before P, before onStop.

If it is actively closed, it will not be saved

The storage mechanism

It is saved to disk and cleared when the activity closes.

Run on the main thread, suitable for saving small objects.

OnSaveInstanceState fragments. SetRetainInstance (true)

Whether to retain the Fragment instance while the Activity is rebuilt. If set to true, the life cycle is called differently

  1. Instead of calling onDestroy(), onDetach() will be called
  2. OnCreate () is not called, but onAttach() and onActivityCreate() are

The storage mechanism

Keep in memory. Can hold complex objects

ViewModel

This is used to restore data when the page is rebuilt after configuration changes

Prepare data for the interface. ViewModel objects are automatically retained during configuration changes. This eliminates the need to re-request data after the page is rebuilt

  1. Views, Lifecycle, or any class that might store a reference to the Activity context cannot be referenced. The ViewModel exists longer than the activity or fragment declaration cycle.

2. Include LifeCycleObservers, such as LiveData. But it cannot observe life-cycle aware observables (livedata.observe)

The life cycle

Normally, this ends with activity.ondestroy (). Fragment. OnDetach ().

The ViewModel is not destroyed for abnormal states, such as Activity rebuilds caused by screen rotation. So you can restore the state via ViweModel

The storage mechanism

Save it in memory. Suitable for storing complex objects.

contrast

Whether ViewModel can replace onSaveInstanceState

There are two cases:

A. OnSaveInstanceState ()

Can’t. But they are used together

  1. The ViewModel saves instances only if the Activity is rebuilt due to configuration changes. If the exit is abnormal, the instance cannot be saved

OnSaveInstanceState can save data after the abnormal state exits. 2. If the rebuild is caused by configuration modification, the ViewModel instance is not rebuilt and can be read from memory. If the abnormal state causes a rebuild, the ViewModel is destroyed. The state can only be obtained from onSaveInstanceState. But onSaveInstanceState cannot hold complex objects. So you can cache data locally in the ViewModel, onSaveInstanceState holds the data Id, and when rebuilt, reads data from local storage based on the Id.

Two fragments. SetRetainInstance onSaveInstanceState (true)

Can replace

  1. In this case, the view inside the Fragment has been destroyed and only the data remains. The ViewModel can also store data
  2. The ViewModel is also implemented internally by calling setRetainInstance(true)

How do ViewModel and onSaveInstanceState() work together

SavedStateRegistry associates onSaveInstanceState() with ViewModel. But it’s actually the Bundle that accesses the data.

Refer to the link

Save interface state

Understand the Activity lifecycle

ViewModels: Persistence, onSaveInstanceState(), Restoring UI State and Loaders

The saved state module of the ViewModel