brig
brig

Reputation: 3773

Purpose of onSaveInstanceState() in activity lifecycle

I have gone through the activity life cycle document and as per document onSaveInstanceState() and onRestoreInstanceState() will be used to preserve the UI state.

To test the usage of the above methods:

Case 1: I have create a simple layout with edit box and toggle button and I have entered some text in my edit text field and changed toggle button to 'on' and then changed the orientation of the activity. To my surprise my activity able to retain the values without saving state in onSaveInstanceState() method.

Case 2: Navigated to other activity and came back to my activity, in this case also its retaining its value.

So when activity able to retain its state then what the purpose of below methods.

onSaveInstanceState()
onRestoreInstanceState()

Upvotes: 0

Views: 665

Answers (3)

NazarK
NazarK

Reputation: 1221

You can watch this video about restoration - https://www.youtube.com/watch?v=ekN2zvFytZk. But in a nutshell android can restore view's state by traversing all views in the hierarchy and get their values(in your case EditText value). And one important thing - views have to have set ids.

These methods can be useful when you want to store variables in your activity. For example - you're implementing book reader and you might want to save view_mode(night_mode/day_mode) that user selected.

Upvotes: 1

Vikram Ezhil
Vikram Ezhil

Reputation: 995

The most common usage of these functions are when your app is killed in the background by the android OS to allocate memory space for other applications.

When the user comes back to your application you would need to restore the last shown views/values to the user. This is done via onSaveInstanceState & onRestoreInstanceState.

@Override
public void onSaveInstanceState(Bundle outState) 
{
    // TODO Auto-generated method stub
    super.onSaveInstanceState(outState);

    // Save the values in a bundle which you would like to restore
    outState.putString("vals", val1);
};

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) 
{
    // TODO Auto-generated method stub
    super.onRestoreInstanceState(savedInstanceState);

   // restore your values here
   val1 = savedInstanceState.getString("vals");
}

Upvotes: 2

said
said

Reputation: 106

The system has a default behavior; it save the state of views that has an ID, this feature is not guaranteed, and in some cases, you must override this method and save the state of your views.

FROM DOC: "The default implementation takes care of most of the UI per-instance state for you by calling onSaveInstanceState() on each view in the hierarchy that has an id, and by saving the id of the currently focused view (all of which is restored by the default implementation of onRestoreInstanceState(Bundle)). If you override this method to save additional information not captured by each individual view, you will likely want to call through to the default implementation, otherwise be prepared to save all of the state of each view yourself."

Upvotes: 1

Related Questions