Activity lifecycle explained in details
Posted / Публикувана 2011-08-09 in category / в категория: Android
|
Warning: count(): Parameter must be an array or an object that implements Countable in /home/bolyarco/www-ikratko/ogrelab/wp-content/plugins/microkids-related-posts/microkids-related-posts.php on line 645
Usually one of the biggest obstacles for the newcommers to android programming is the correct understanding of the activity's lifecycle, for example what is the difference in the event's chain when hitting "Back" button compared to activity being closed when phone is rotated. You cannot obtain this information by looking just at the activity lifecycle diagram in the API ref. In the following post I will use sample project that uses logging to demonstrate exactly what happens in the different cases. It is ment to be used as complementary tool for understanding how activities work.
Before proceeding you may want to download the sample project: Activity Lifecycle Demo. It contains simple logging of the activity and method name in each of the methods related to the lifecycle.
1. "Cold" start
Cold start is the case when the activity was a) started for the first time OR b) started after stopped with hitting "Back" button (the hardware one not <Button> that may exists in some activity's layout).
Logcat will show something like (timestamps will differ):
08-09 17:37:20.500: INFO/TEST(10107): A onCreate 08-09 17:37:20.507: INFO/TEST(10107): A onStart 08-09 17:37:20.507: INFO/TEST(10107): A onResume
Important to note here is that when onCreate(Bundle savedInstanceState)
is called savedInstanceState
is null. That is because there is no saved state because this is "cold" start (obviously).
2. Hitting "Back" button
08-09 17:50:19.050: INFO/TEST(10107): A onPause 08-09 17:50:19.433: INFO/TEST(10107): A onStop 08-09 17:50:19.433: INFO/TEST(10107): A onDestroy | isFinishing: true
Important to note here is isFinishing: true
which means that call to isFinishing()
in the onDestroy()
returns true, i.e. which happens when:
- user hits "back" button OR
- activity's code calls it's
finish()
(isFinishing()
returns false when activity is geting closed after phone rotaion in order to be started again)
3. Another activity comes in front of the our activity
There are two variations here:
3.1. Fullscreen non-transparant activity comes in front (i.e normal activity)
In the sample project this happens when:
- "home" button is pressed, i.e. home screen activity is shown OR
- "home" button is longpressed and user switches to another activity OR
- user clicks on "Start activity B" (logcat shown bellow, includes log messages from activity B)
08-09 18:30:37.085: INFO/TEST(10107): A onSaveInstanceState 08-09 18:30:37.085: INFO/TEST(10107): A onPause 08-09 18:30:37.109: INFO/TEST(10107): B onCreate 08-09 18:30:37.109: INFO/TEST(10107): B onStart 08-09 18:30:37.109: INFO/TEST(10107): B onResume 08-09 18:30:37.382: INFO/TEST(10107): A onStop
Important to note here is that there is no guarantee if onSaveInstanceState
will be called before or after onPause
. onSaveInstanceState
is the place where you may want to save some data in order to restore it when (and if) activity is restarted.
Next time out activity "A" comes in front logcat will show:
08-09 18:36:27.320: INFO/TEST(10107): A onRestart 08-09 18:36:27.320: INFO/TEST(10107): A onStart 08-09 18:36:27.320: INFO/TEST(10107): A onResume
i.e. this is the so called "long" path of reshowing which includes calls to onRestart
and onStart
.
3.2. Non-fullscreen transparant activity comes in front
Example for non-fullscreen transparant activity is activity that have android:theme="@android:style/Theme.Dialog"
in the manifest. In the sample project you can start such activity by clicking on "Start activity D" button.
08-09 18:29:49.023: INFO/TEST(10107): A onSaveInstanceState 08-09 18:29:49.023: INFO/TEST(10107): A onPause 08-09 18:29:49.043: INFO/TEST(10107): D onCreate 08-09 18:29:49.043: INFO/TEST(10107): D onStart 08-09 18:29:49.043: INFO/TEST(10107): D onResume
Important to note here is that there is no call to A onStop
which means that activity "A" is not stopped because it is seen trough the transparant activity "D".
When in activity "D" if you hit the "Back" button you will see:
08-09 18:34:43.105: INFO/TEST(10107): D onPause 08-09 18:34:43.121: INFO/TEST(10107): A onResume 08-09 18:34:43.140: INFO/TEST(10107): D onStop 08-09 18:34:43.140: INFO/TEST(10107): D onDestroy | isFinishing: true
This is so called "short" path of reshowing which includes just call to onResume
. One other example of the "short" path is when we try to start another activity but it fails to get in front because it force-closes or calls finish()
before get in front (like in onCreate). You can go trough this case by clicking on "Start activity C" button.
4. "Warm" start
"Warm" start happens when your activity was first "Cold" started, some other activity (fullscreen, non-transparent) got moved in front and then your activity was moved back in front.
08-09 18:46:03.636: INFO/TEST(10107): A onRestart 08-09 18:46:03.636: INFO/TEST(10107): A onStart 08-09 18:46:03.636: INFO/TEST(10107): A onResume
Main difference between "Cold" and "Warm" start is that with the "Warm" start onCreate
is not called but onRestart
is called instead.
5. Stopping and restarting on orientation change
By default when the device is rotated your activity will be stopped and then started again. Logcat looks like this:
08-09 22:32:05.628: INFO/TEST(10107): A onSaveInstanceState 08-09 22:32:05.628: INFO/TEST(10107): A onPause 08-09 22:32:05.628: INFO/TEST(10107): A onStop 08-09 22:32:05.628: INFO/TEST(10107): A onDestroy | isFinishing: false 08-09 22:32:05.730: INFO/TEST(10107): A onCreate 08-09 22:32:05.734: INFO/TEST(10107): A onStart 08-09 22:32:05.734: INFO/TEST(10107): A onRestoreInstanceState 08-09 22:32:05.734: INFO/TEST(10107): A onResume
Important to note here are three things:
onSaveInstanceState
is called that gives you a chance to save activity's stateisFinishing
returns false- when activity is recreated the
onRestoreInstanceState
is called and it receives the bundle saved inonSaveInstanceState
Common mistake made by new android programmers (including me) is to expect that onSaveInstanceState
and onRestoreInstanceState
are paired, i.e. if onSaveInstanceState
is called on stopping then onRestoreInstanceState
will be called too on starting. This is NOT the case. onRestoreInstanceState
is called only if application is beign killed by the OS. Most of the time you will do the state recovery in onCreate
(which receives the same bundle saved in onSaveInstanceState
) and not in onRestoreInstanceState
. As stated in the documentation of onRestoreInstanceState:
Most implementations will simply use
onCreate(Bundle)
to restore their state, but it is sometimes convenient to do it here after all of the initialization has been done or to allow subclasses to decide whether to use your default implementation.
6. Application started after being killed by the OS
This may happen because of two reasons:
- android OS needs more memory at some point and kills you app in order to reclaim it's memory;
- you use app like Advanced Task Killer to kill the app.
|
September 21st, 2012 at 10:07:42
very informative, nice to know the flow of activities
October 22nd, 2012 at 12:57:47
The best explanation about about android activity life cycle I have ever seen.Now I know something about the life cycle. Thank you very much
January 4th, 2013 at 04:31:29
Excellent tutorial …….. keep the good work up :)