Android Save and Restore Instance State: A Writeup about My Journey into the Unknown.

Dreamt.it
3 min readAug 3, 2021

Today or should I say this week, I came across a topic during my “Become the best android developer” goal. The topic is the onSaveInstanceState and onRestoreInstanceState and how it can be used to save the current state of our UI across device configuration change, What this means is if we had updated the UI with let’s say data from an API, and the user decides to rotate the device, during the rotation our activity will be destroyed and the onCreate() will be called again but this time in landscape mode, when this happens all the current state of the UI will be lost unless saved and that when the Super Heroes come to save the day, the onsaveInstanceState() and the onRestoreInstanceState() if called upon will save the day.

onSaveInstanceState

What is the onSaveInstancestate() you might wonder? Well according to google

The onSaveInstanceState() callback stores data needed to reload the state of a UI controller, such as an activity or a fragment, if the system destroys and later recreates that controller.

In English “It is where we save the current state of the UI”.

onRestoreInstanceState

The onRestoreInstanceState() callback resources the data that was previously stored within the bundle.

If you’ve been keeping up, we’ve established what the onSaveInstanceState() is and what the onRestoreInstanceState() is, in summary they are both callback methods that are used to navigate or solve the “device configuration” change problem.

You might be wondering how does the onSaveInstanceState() and the onRestoreInstanceState() store the data? How does the onRestoreInstanceState() suddenly have data that it can restore ? I had these burning questions.

Here’s a code snippet of the onSaveInstanceState() and onRestoreInstanceState()

override fun onSaveInstanceState(outState: Bundle) {

super.onSaveInstanceState(outState)

override fun onRestoreInstanceState(savedInstanceState: Bundle) {

super.onRestoreInstanceState(savedInstanceState)

By narrowing down our attention we can find they both have something in common within the constructor (Bundle), huh? Bundle, yes Bundle what is a Bundle ?

Bundle is a utility class that lets you store a set of name-value pairs. Into a Bundle object, you can put integers, longs, strings, arrays, etc along with the keys to identify them. When needed, these values can be obtained by using those keys.

Hmmm ok I get it now, that’s how the UI state in stored in the onSaveInstanceState(), when the method is called the data is stored using a key into a bundle and a bundle is a Map, which means along with our data we have to specify a key for that data, aha! So that’s how we restore it, within the onRestoreInstanceState() we just call the key that was used to save that data into the Bundle.

HEY IT’S ME FROM MAYBE A MINUTE INTO YOUR READ JUST CHECKING TO SEE IF YOUR STILL WITH ME…….

Hopefully you also had an aha! Moment, but I was not satisfied. “how is the onRestoreInstanceState() even aware of the Bundle?” for all I know i might be referencing a totally different Bundle, don’t quote me on this one i’d advise you to perform deeper research on the topic but here’s my hypothesis as to why, whenever the onSaveInstanceState() and the onRestoreInstance() states are called , the IDE imports this “import android.os.Bundle” so this made me to conclude maybe the Bundle leaves outside of our Activity, Fragment and ultimately our application, it resides in the os that’s why we are able to access the Bundle that matched the key we needed.

With all of the technical mumbo jumbo out the way and hopefully digested, i understood onSaveInstanceState() and onRestoreInstanceState() better, i knew what problem it solved and what each part was suppose to perform, the next step for me was writing out the code and learning different scenarios of when? , how? What type of data? The drawbacks? The advantages? Performance? Of using the onSaveInstanceState() and onRestoreInstanceState(). I know I'm a man with a lot of questions…….But I'll be answering these questions. Hopefully.

i’d like to send a special thank you to all of you who read, and clapped on my last article it meant a lot to me, and to you my current reader who made it this far, I thank you, for choosing my article to be a light in your lost time in your search for knowledge, “it get’s hard, but that’s how it get’s easy”, Thank never forget, “You can become the best at what you apply your time and effort into”.

--

--

Dreamt.it

Tech enthusiast documenting my learning journey in software, hardware, and books. Discover practical tips and tricks for a seamless tech experience. 🚀