122

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.

azizbekian
  • 60,783
  • 13
  • 169
  • 249
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
  • got final answer from Android official document, onSaveInstanceState() called between onPause() and onStop(). – Rishi Nov 14 '17 at 17:23
  • 1
    @Rishi Can you please provide a link to that document? – Luis Mendo Nov 14 '17 at 17:31
  • https://developer.android.com/guide/components/activities/activity-lifecycle.html – Rishi Nov 14 '17 at 18:17
  • read Save your activity state Paragraph there – Rishi Nov 14 '17 at 18:18
  • @Rishi I haven't found the answer in that paragraph. In which part is it exactly? – Luis Mendo Nov 29 '17 at 20:07
  • @Rishi 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. – The incredible Jan Feb 14 '19 at 12:57

5 Answers5

126

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().

tir38
  • 9,810
  • 10
  • 64
  • 107
Steve M
  • 9,296
  • 11
  • 49
  • 98
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

Android Developer
  • 9,157
  • 18
  • 82
  • 139
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

Community
  • 1
  • 1
azizbekian
  • 60,783
  • 13
  • 169
  • 249
  • 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 ? – Filipkowicz Apr 05 '19 at 12:37
  • @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. – azizbekian Apr 07 '19 at 09:44
  • 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. – Sandhu Aug 31 '19 at 04:45
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.

Mahmoud Mzz
  • 71
  • 1
  • 1
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);
} 
Sudheesh Mohan
  • 2,560
  • 3
  • 21
  • 38
parvez rafi
  • 464
  • 4
  • 20