You Know Who
You Know Who

Reputation: 105

why we need onSaveInstanceState()?

In Activity Lifecycle, we can use onPause() to save screen state and release some resources. On the other side, we can use onResume() to get previously saved state and reuse previous variables.

So, why we need using onSaveInstanceState() ?

Upvotes: 0

Views: 344

Answers (3)

guipivoto
guipivoto

Reputation: 18677

I understand your question. However, those method has different proposes. There're also some differences between onPause/onResume and onSaveInstanceState that are important to remember:

You don't need to store the state everytime

onSavedInstanceState() is not called when you press Home Key or press back key to leave the activity. onPause, on the other hand is called on those situations. So, saving the state during onPause is a lack of resources (your are storing the state when it is not necessary).

onResume runs too late

onResume() is called after your activity is already ready to be displayed to the user (since it is called after onStart and onCreate). So, restoring the view state during onResume is not a good ideia because it is already too late...

During onCreate, you already can access the Bundle previously saved. So, inside onCreate, you can decide if you set default content or restore the old-stored data. You can update the view before the view is even visible. User won't notice the change.

If you restore the state during onResume, the view was already created and probably already visible to the user. So, he will notice the screen being re-created with default content and slowly change to the old-stored content.

Updating view state during onResume can me the screen blink

onSavedInstanceState() allows you to store the info you need in a Bundle. Then, during onCreate, you already can access that bundle and "create" the screen with proper content. Before the content is visible to the user.

If you try to restore whole screen state during onResume, you can notice the screen blinking. This will happen because whole screen was created during onCreate and later was updated again during onResume.. Even if you aren't changing the content.

Those are some of the reasons I see you really should use onSaveInstanceState to store the view state.

Upvotes: 1

Asad Ali Choudhry
Asad Ali Choudhry

Reputation: 5261

onSaveInstanceState() gets called automatically and thats not the part of typical (general flow) activity lifecycle. Its called automatically by the OS when your App is in background and OS needs to kill your App to free some memory. And you also receive a Bundle object in it, you can put your important data in that bundle. And You receive that bundle in OnCreate() method when Users resumes your App from Background.

You can read about it further here

Example by Android

static final String STATE_SCORE = "playerScore";
static final String STATE_LEVEL = "playerLevel";
// ...


@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save the user's current game state
    savedInstanceState.putInt(STATE_SCORE, currentScore);
    savedInstanceState.putInt(STATE_LEVEL, currentLevel);

    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
}

Now in OnCreate Method you receive that bundle

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); // Always call the superclass first

    // Check whether we're recreating a previously destroyed instance
    if (savedInstanceState != null) {
        // Restore value of members from saved state
        currentScore = savedInstanceState.getInt(STATE_SCORE);
        currentLevel = savedInstanceState.getInt(STATE_LEVEL);
    } else {
        // Probably initialize members with default values for a new instance
    }
    // ...
}

Upvotes: 0

Gal
Gal

Reputation: 422

onSaveInstanceState() Is a function that is called as part of the lifecycle. The most common example of it is when you rotate screen and the activity gets destroyed and recreated on save instance is being called. when it is called you can insert values (in key value format) to a bundle and then restore the activity as it was.

for example if I have a timer going down and I rotate the phone the screen will be recreated and the timer will restart unless I save it state with onSaveInstaceState()

Upvotes: 0

Related Questions