Why Activity.onDestroy() and Activity.onRestoreInstanceState() are not always invoked

First, the facts:

  1. Activity.onStop() is not always followed by Activity.onDestroy() since Android can decide to kill your application just after invoking Activity.onStop()
  2. Activity.onSaveInstanceState() is not always followed by Activity.onRestoreInstanceState()

1. When is Activity.onDestroy() invoked ?

First, check the Android activity lifecycle: as you can see, Android can kill your application without invoking onDestroy() if “an app with higher priority needs memory“.

In fact, the javadoc for onDestroy() also states that “There are situations where the system will simply kill the activity’s hosting process without calling this method (or any others) in it, so it should not be used to do things that are intended to remain around after the process goes away“.

I wrote a simple application containing only one (main) activity and used a couple of scenarios to have a better understanding of when onDestroy() was called:

Scenario #1-1: HOME key is hit while the activity is running

  • Android invokes onSaveInstanceState() -> onPause() -> onStop()
  • You are now back on the HOME screen
  • When the application is relaunched, Android invokes onCreate() -> onStart() -> onResume()

Note: onCreate() may actually not be invoked when the application is started again because it’s up to the Android OS to decide whether or not to kill your activity just after invoking onStop(). So if onCreate() is not invoked, this simply means that your activity was still in the background in a “stopped” state.

Scenario #1-2: BACK key is hit while the activity is running

  • Android invokes onPause() -> onStop() -> onDestroy()
  • You go back to the screen you were before launching the application
  • When the application is relaunched, Android invokes onCreate() -> onStart() -> onResume()

Note: this time, onCreate() is always invoked because hitting the BACK key destroys your activity.

Scenario #1-3: screen orientation changes while the activity is running (CTRL-F12 in the Android emulator)

  • Android invokes onSaveInstanceState() -> onPause() -> onStop() -> onDestroy()
  • It then invokes: onCreate() -> onStart() -> onRestoreInstanceState() -> onResume()

Scenario #1-4: another application is launched while the activity is running (click on the phone button in the Android emulator)

  • Android invokes onSaveInstanceState() -> onPause() -> onStop()
  • If you now hit BACK to come back to your application: onStart() -> onResume()

 

2. When is Activity.onRestoreInstanceState() invoked ?

If you check again the various scenarios above, you will see that onSaveInstanceState() is indeed not always followed by onRestoreInstanceState().

Why ?

Because there are only 2 scenarios where onSaveInstanceState() is followed by onRestoreInstanceState():

  1. The screen orientation changes (see scenario #1-3)
  2. Another activity gets the focus and then Android decides to kill the previous one (possibly because the device is low on resources)

To test#2 using the Android emulator, you need to do this:

  • Launch your application so that your activity is running and has the focus
  • Launch another application (for ex: click on the phone button in the Android emulator to launch the dialer application)
  • Launch an ADB shell and use the “ps” command to kill your application
  • From the dialer application, hit the BACK button
  • Android will invoke onCreate() -> onStart() -> onRestoreInstanceState -> onResume()

 

Laurent KUBASKI

Advertisements

About lkubaski
www.kubaski.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: