Handling Activity state using Saved Instance State Bundle

Author: | Posted in Android, Quick Tips No comments

In this tutorial we will learn about handling Activity state using Saved Instance State in Android or in other words we can say How to retain Activity state with the help of Saved Instance State Bundle in Android. Because some time we need our application to run on both orientation, User can either use landscape mode or portrait mode and user can also change orientation during run time.

But when the device’s screen orientation is changed the current foreground Activity is restarted in this condition means whole Activity is destroyed and  then recreated. So before Activity will destroyed , We need to save the state of it and when Activity is restarted again we need to restore the state of it.(By saying save the state of activity means saving state of EditText values, TextView values or all data on activity components which we have set programmatically.)

This functionality can be achieved using the onSaveInstanceState and onRestoreInstanceState method of Activity respectively.

First we will save Activity state using onSaveInstanceState and then restore Activity state using onRestoreInstanceState. We will understand it using a small example in this example we will show how we are saving an EditText value while orientation change using above methods.

Create an Android project and add an EditText to a layout file (eg. activity_main.xml) as below

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="@+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>

And use it in your Activity class (eg. MainActivity.java) as below.

public class MainActivity extends Activity {
EditText main_text;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
main_text = (TextView) findViewById(R.id.edittext);
}
}

Now run your application and then type some text in EditText and change orientation of your device. You can see the value is gone from EditText, To retain value we need to add onSaveInstanceState and onRestoreInstanceState methods to our Activity class as below.

public class MainActivity extends Activity {
EditText main_text;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
main_text = (TextView) findViewById(R.id.edittext);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("Value", main_text.getText().toString());
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
main_text.setText(savedInstanceState.getString("Value"));
}
}

After adding these two methods run your application again then type in EditText and change orientation of device now you can see that the value in EditText is not cleared.

Hope this tutorial will help some guys to sort out orientation change problem in their Android applications.