How to persist Data when activity is re-created in Android Studio?

Reghill J Manuelraj
android-core
Published in
3 min readMay 8, 2020

I was wondering for a while how to persist the data in an activity when the activity lifecycle is completed and recreated. Finally, answer found!

Activity

For those who are new to Android Development, an activity could be the single screen that the user views in an application. For every navigation from one screen to another within an application, the user navigates from one activity to another.

When activity would be recreated?

It is important to understand the lifecycle of an activity. When I use the word recreation, it implies that it is already created at least once. Hence, activity recreation would happen when you come back to a particular activity by pressing back button or it might get recreated when you try to change the orientation of your application in your target device(from landscape to portrait or vice versa).

What is the impact of recreation of an activity?

Let’s consider, you have an activity which gets Login details from the user- Username and password. Now the user enters all the details and switches the orientation of the mobile from portrait to landscape.

Guess what? The activity would be destroyed and recreated. This means all the data that the user has entered is lost.

Hence, it is essential to make sure that the data is not lost whenever the activity is recreated.

Saved instance State

The idea of achieving this persistence of data is to pass the data as a bundle in saved instance state. Let’s see how to implement it.

First, in your activity class, implement an override method named onSaveInstanceState which takes a Bundle as a parameter. Here we are trying to put all the data into this bundle parameter(named as outstate in the example below).

In the below example, I am trying to persist a string data. Let’s consider a global class variable sampleString for that purpose. Assume that the variable has some value associated with it.

The sampleString variable has the string value and I am storing it in a new variable named RESTORED_STRING. Now the RESTORED_STRING is bundled in outState.

Now that the bundling is done, we need to unwrap the bundle to get the data whenever the activity is recreated.

The onCreate() method is the first method to be called in the activity life cycle. Thus it is time to unwrap the data. This method takes in a Bundle parameter savedInstanceState. I am trying to unwrap the bundle and assign it back to the sampleString.

Note that, I have used an alternate string here. This is because, not always the Bundle holds data.

Whenever the Bundle variable savedInstanceState is null, I wanted my string to hold some alternate string value. I did that using the Elvis operator to assign the alternative value which is R.string.anyString.

Hurray! Well done . Your data is safe even when the activity vanishes and reappears.

Happy Developing !

--

--

Reghill J Manuelraj
android-core

An heuristic Application Developer with a hobby of scribbling my experiences