When exactly are onSaveInstanceState() and onRestoreInstanceState() called?
Asked Answered
R

5

124

The following figure (from the official doc) describes the well-known lifecycle of an Android activity:

enter image description here

On the other hand, when the activity is destroyed by the system (for example because memory needs to be reclaimed), the state of the activity is sometimes automatically saved and restored by means of the methods onSaveInstanceState() and onRestoreInstanceState(), as illustrated by the following figure (also from the official doc):

enter image description here

I'm aware that onSaveInstanceState() is not always called when an activity is about to be destroyed. For example, if it is destroyed because the user has pressed the "back" button, the activity state is not preserved. But in the cases when the state is saved and restored, and onSaveInstanceState() / onRestoreInstanceState() get called, when exactly are they called?

For example, according to the above figures, onRestoreInstanceState() might be called before onStart(), or after onStart() but before onResume(), or after onResume(). Similarly, several possibilities exist for onSaveInstanceState(). So when are they called exactly?

Ideally, what I would like is to see a combined diagram showing the activity lifecycle states and the save/restore methods, if that exists.

Ranie answered 30/12, 2013 at 0:43 Comment(6)
got final answer from Android official document, onSaveInstanceState() called between onPause() and onStop().Idalia
@Idalia Can you please provide a link to that document?Ranie
developer.android.com/guide/components/activities/…Idalia
read Save your activity state Paragraph thereIdalia
@Idalia I haven't found the answer in that paragraph. In which part is it exactly?Ranie
@Idalia You should have proved your statement with a quote. But you probably could not because it was never in the document. onSaveInstanceState() can be called before onPause(). Google says: "As your activity begins to stop, the system calls the onSaveInstanceState() method..." - it is called before onStop(). There is no guarantee that onPause() took place before.Huntington
E
127

Per the documentation:

void onRestoreInstanceState (Bundle savedInstanceState)

This method is called between onStart() and onPostCreate(Bundle).

void onSaveInstanceState (Bundle outState)

If called, this method will occur after onStop() for applications targeting platforms starting with Build.VERSION_CODES.P. For applications targeting earlier platform versions this method will occur before onStop() and there are no guarantees about whether it will occur before or after onPause().

Embassy answered 30/12, 2013 at 1:5 Comment(11)
Thanks. Could you please provide the links to the documentation?Ranie
There you go, also I don't think there is anything else between onStart() and onPostCreate(), so onRestoreInstanceState() is well defined in the chain.Embassy
Thanks a lot. This clarifies the issueRanie
@SteveM "There are no guarantees about whether it will occur before or after onPause()" Does that mean if we try to access a view (to get some value to save, like an index from a listview) we might run into NullPointerExceptions?Dab
Then, what is recommended, saving a data structure in onPause and restoring it in onResume instead in onSaveInstanceState and onRestoreInstanceState?Meara
@Gödel77 Saving in onPause() is the only sure fire way to make sure data gets saved, onSaveInstanceState() isn't always called (see below) and your app could be killed in meantime.Embassy
APMO It calls only when heavy memory operations performKaroline
what is the difference between called between and occur before ?Isooctane
@aeid Is that a serious question?Huntington
@TheincredibleJan I’m no so sure, that was a long time ago, i have moved away from Android dev.Isooctane
So if i fire an intent when app is in background will the onsaveinstance gets called for the newActivity?Elutriate
C
22

As per doc1 and doc2

onSaveInstanceState

Prior to Honeycomb, activities were not considered killable until after they had been paused, meaning that onSaveInstanceState() was called immediately before onPause(). Beginning with Honeycomb, however, Activities are considered to be killable only after they have been stopped, meaning that onSaveInstanceState() will now be called before onStop() instead of immediately before onPause().

onRestoreInstanceState

This method is called between onStart() and onPostCreate(Bundle) when the activity is being re-initialized from a previously saved state

Consultation answered 6/1, 2017 at 10:9 Comment(1)
liked the way you described scenarios over different Android versionsCembalo
P
19

In addition to already posted answers, there is a subtle change introduced in Android P, which is:

void onSaveInstanceState(Bundle outState)

If called, this method will occur AFTER onStop() for applications targeting platforms starting with P. For applications targeting earlier platform versions this method will occur before onStop() and there are no guarantees about whether it will occur before or after onPause().

Source: docs

As to why this change is introduced, here's the answer:

... so an application may safely perform fragment transactions in onStop() and will be able to save persistent state later.

Source: docs

Pau answered 12/3, 2018 at 8:21 Comment(3)
Hi, great comment. Do you know how will behave app that target P but runs on lower api ? Same as app targeting lower api or it will be consistent across api and keep "targeting api" behaviour ?Cavetto
@Filipkowicz, Do you know how will behave app that target P but runs on lower api? As long as the app is being run on say M, then the version of Android that this device has doesn't contain changes, that are introduced in P, which means that regardless you have specified target as P you won't see different for pre-P devices. Hope this answers your question.Pau
I feel so relaxed today after reading this answer, because I was doing the android free course on Udacity and they still have the old version of the tutorials which state in the lesson 5 exercise 8 that the onStop and onDestroy methods should not be there in the displayed textView. But I did not know that was for the older versions of android and I was running my application on android pie and was getting onStop method in my textView. Thank you so much. Finally feeling good.Buyers
A
7

This is an extra information for onSaveInstanceState(Bundle)

from docs

Do not confuse this method with activity lifecycle callbacks such as onPause(), which is always called when an activity is being placed in the background or on its way to destruction, or onStop() which is called before destruction. One example of when onPause() and onStop() is called and not this method is when a user navigates back from activity B to activity A: there is no need to call onSaveInstanceState(Bundle) on B because that particular instance will never be restored, so the system avoids calling it. An example when onPause() is called and not onSaveInstanceState(Bundle) is when activity B is launched in front of activity A: the system may avoid calling onSaveInstanceState(Bundle) on activity A if it isn't killed during the lifetime of B since the state of the user interface of A will stay intact.

So it's default implementation for..

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.

Adalia answered 13/3, 2017 at 9:26 Comment(0)
B
1
String activityState;
@Override 
public void onCreate(Bundle savedInstanceState) {
// call the super class onCreate to complete the creation of activity like 
// the view hierarchy 
super.onCreate(savedInstanceState);

// recovering the instance state 
if (savedInstanceState != null) {
     activityState = savedInstanceState.getString(STATE_KEY);
 } 

   setContentView(R.layout.main_activity);
   mTextView = (TextView) findViewById(R.id.text_view);
} 

//This callback is called only when there is a saved instance previously saved using //onSaveInstanceState(). We restore some state in onCreate() while we can optionally restore // other state here, possibly usable after onStart() has completed. // The savedInstanceState Bundle is same as the one used in onCreate().

@Override 
public void onRestoreInstanceState(Bundle savedInstanceState) {
 mTextView.setText(savedInstanceState.getString(STATE_KEY));
  } 


// invoked when the activity may be temporarily destroyed, save the instance 
//state here 
//this method will be called before onstop

@Override 
 public void onSaveInstanceState(Bundle outState) {
    outState.putString(STATE_KEY, activityState);

    // call superclass to save any view hierarchy 
    super.onSaveInstanceState(outState);
} 
Brachial answered 4/10, 2017 at 8:48 Comment(1)
Excuse me, how does this answer the question when are the save/restore methods called, exactly?Ranie

© 2022 - 2024 — McMap. All rights reserved.