SaveStageRegistry is androidx savedstate: savedstate repository for developer tools, androidx FragmentActivity and fragments are adaptation of the tool.

role

Developers can easily sense the timing of state saving (staging) and its data recovery in the context of the conditions.

usage

  • To obtainSaveStageRegistryObject instance, which can be passedSavedStateRegistryOwner.getorComponentActivity.getSavedStateRegistry
  • Through instance object injection (registerSavedStateProvider) own Provider (SavedStateProvider). This Provider will be triggered before the component is reworked. The Bundle returned by the developer is mapped to the key provided at registration time and placed in the component’s total cache
  • In the corresponding life cycle, e.gLifecycleObserver.onStateChangedInvokes when triggeredconsumeRestoredStateForKeyTake out the Bundle you buried and make your own purchases
  • You can also callrunOnNextRecreationInject a class that implementsAutoRecreatedThe Consume interface indicates that it will be triggered in the next rewrite, and can be used to obtain the relevant Bundle data when the consume interface is called back

aboutSavedStateRegistryOwner.get

The Jetpack Compose implementation, will be called ComponentActivity. SetOwners extension methods, will be injected SavedStateRegistryOwner. The injected object is a DecorView by calling its setTag with r.i.D.view_tree_saved_state_registry_OWNER as the key. When you get, you layer back through the View (via view.getparent ()), so don’t worry if you’ve called the setOwners method, you’ll always get it. For now, only Jetpack Compose is called in the source code. Otherwise, the developer needs to call it by itself if necessary.

Principle of Recreation

  1. Components in AndroidX, which already have one embedded insideSavedStateRegistryControllerObject that is called during the staging and recovery cycle of the native component
  2. When it resumes, a lifecycle Observer is injected, namelyRecreator
  3. whileReacratorWill be inonStateChangedWhen the reflection builder passes throughrunOnNextRecreationInjected class, and triggered itonRecreatedmethods