Skip to content

Configuration change and process death

diklimchuk edited this page May 25, 2021 · 3 revisions

Configuration change

There are some cases when you need to restore the screen while the app is still running. For example, after a configuration change. There's no built-in mechanism for handling it, but you can easily implement it yourself. Just reuse the Store. You can save it in a Dagger scope of simply keep a reference to it inside and Application class.

Process death

A more complicated case is when you need to restore the app from savedInstanceState. Once again, there's no out-of-the-box support for it. But implementation is fairly simple, just save the state of your Store:

  override fun createStore() = ElmStore(
    // NOTE: State is loaded from savedStateRegistry:
    savedStateRegistry.consumeRestoredStateForKey("STATE")?.getSerializable("STATE") as SomeState? ?: SomeState(),
    SomeActor,
    SomeReducer
  }

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    // NOTE: State is saved to savedStateRegistry:
    savedStateRegistry.registerSavedStateProvider("STATE") { Bundle().apply { putSerializable("STATE", store.currentState) } }
  }