Android onSaveInstanceState onRestoreInstanceState With Example

In this tutorial, we’ll be discussing the two vital methods for managing the state of the application, namely onSaveInstanceState and onRestoreInstanceState.

We’ll be developing a Counter Android Application in which we’ll handle the state of the application when the configuration changes.

Android Lifecycle

Following is the lifecycle of the Activities in our application:

android-lifecycle (1)

Whenever there is a configuration change, such as rotation or application going into multi-window mode, the activity is recreated.

In this recreation, the application gets restarted and may lose data in the views if not handled properly.

For this there are two methods that are triggered at different stages of the lifecycle:

  • onSaveInstanceState
  • onRestoreInstanceState

They are used to save and retrieve values. The values are stored in the form of a key-value pair.
Let’s look at each of them separately.

onSaveInstanceState

onSaveInstanceState method gets called typically before/after onStop() is called. This varies from Android version to version. In the older versions it used to get before onStop().

Inside this method, we save the important values in the Bundle in the form of key value pairs.

The onSaveInstanceState method looks like :

On the outState bundle instance we add the key value pairs. Following are the methods applicable:

android-lifecycle (1)

We can pass custom class instances by setting the class as Parcelable or Serializable.

Let’s look at an example using Parcelable since it is faster than Serializable.

Following is a custom class :

writeToParcel method is where we set the class properties on the Parcelable instance.

Now create an instance of the Model and save it in onSaveInstanceState.

Now we can retrieve these saved values in the onRestoreInstanceState method.

Note: onSaveInstanceState gets called whenever you press the home button in your application as well.Another note: Things like EditText can save and restore their content implicitly provided you’ve set an id on the View. Activity will automatically collect View’s State from every single View in the View hierarchy.

onRestoreInstanceState

This method gets triggered only when something was saved in onSaveInstanceState method.
It gets called after onStart().

Following are the other method available:

android-save-restore-instance-state-project-structure (1)

The super method implementation restores the view hierarchy.

Generally, onRestoreInstanceState isn’t used to restore values often now. The same can be done from the Bundle in the onCreate method. Also since the onCreate method is always called before, it is a good practice to restore the saved instances there only.

In the following section, we’ll be creating a Counter Application in which we’ll save the state of the counter using the above methods.

Project Structure

android-save-restore-instance-state-project-structure (1)

Code

The code for the activity_main.xml is given below:

In the above layout, the second EditText doesn’t have any id set. So when the device is rotated or any configuration happens that causes the activity to recreate, this EditText contents would be reset.

The code for the MainActivity.java is given below:

In this the save the integer value of the counter and then when the activity is recreated, the onCreate gets called again. Over there we check if the savedInstanceState Bundle is null or not. If it isn’t we restore the views with the data from the counter.

The output of the application in action is given below:

android save restore instance state

Notice that the Phone Number field goes empty when the orientation was changed.

This brings an end to this tutorial. You can download the project from the link below.

By admin

Leave a Reply

%d bloggers like this: