Learning Android Development

Making Saving Android Application State Possible

Other than Activity and Fragment, how can one save a state at the Application level?

Photo by Sasun Bughdaryan on Unsplash

If you have worked on Android for a while, you should know that on both Activity and Fragment, when the app is killed by the system, we can keep the state, and restore it later, using the function below.

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
}

What if we want to save the state at the Application level? There’s no onSaveInstancesState in the Application. 🧐

Maybe the system only kills the Activity and Fragment? Does it kill the Application too? Unfortunately, yes.

When we turn on Don't Keep Activity in the Developer Option Setting, the emulated situation is just the Activity is killed, but not Application. But such a case does happen often when you have a large user base using your App.

Fortunately, you can imitate Application-level killed by the system as outlined by this blog.

--

--