What is the life cycle of an Android activity? Why are so many similar sounding methods (onCreate()
, onStart()
, onResume()
) called during initialization, and so many others (onPause()
, onStop()
, onDestroy()
) called at the end?
When are these methods called, and how should they be used properly?
See it in Activity Lifecycle (at Android Developers).
onCreate():
onRestart():
onStart():
onResume():
onPause ():
onStop():
onDestroy():
When the Activity first time loads the events are called as below:
When you click on Phone button the Activity goes to the background and the below events are called:
Exit the phone dialer and the below events will be called:
When you click the back button OR try to finish() the activity the events are called as below:
Activity States
The Android OS uses a priority queue to assist in managing activities running on the device. Based on the state a particular Android activity is in, it will be assigned a certain priority within the OS. This priority system helps Android identify activities that are no longer in use, allowing the OS to reclaim memory and resources. The following diagram illustrates the states an activity can go through, during its lifetime:
These states can be broken into three main groups as follows:
Active or Running - Activities are considered active or running if they are in the foreground, also known as the top of the activity stack. This is considered the highest priority activity in the Android Activity stack, and as such will only be killed by the OS in extreme situations, such as if the activity tries to use more memory than is available on the device as this could cause the UI to become unresponsive.
Paused - When the device goes to sleep, or an activity is still visible but partially hidden by a new, non-full-sized or transparent activity, the activity is considered paused. Paused activities are still alive, that is, they maintain all state and member information, and remain attached to the window manager. This is considered to be the second highest priority activity in the Android Activity stack and, as such, will only be killed by the OS if killing this activity will satisfy the resource requirements needed to keep the Active/Running Activity stable and responsive.
Stopped - Activities that are completely obscured by another activity are considered stopped or in the background. Stopped activities still try to retain their state and member information for as long as possible, but stopped activities are considered to be the lowest priority of the three states and, as such, the OS will kill activities in this state first to satisfy the resource requirements of higher priority activities.
*Sample activity to understand the life cycle**
Adding some more info on top of highly rated answer (Added additional section of KILLABLE and next set of methods, which are going to be called in the life cycle):
Source: developer.android.com
Note the "Killable" column in the above table -- for those methods that are marked as being killable, after that method returns the process hosting the activity may be killed by the system at any time without another line of its code being executed.
Because of this, you should use the
onPause()
method to write any persistent data (such as user edits) to storage. In addition, the methodonSaveInstanceState(Bundle)
is called before placing the activity in such a background state, allowing you to save away any dynamic instance state in your activity into the givenBundle
, to be later received inonCreate(Bundle)
if the activity needs to be re-created.Note that it is important to save persistent data in
onPause()
instead ofonSaveInstanceState(Bundle)
because the latter is not part of the lifecycle callbacks, so will not be called in every situation as described in its documentation.I would like to add few more methods. These are not listed as life cycle methods but they will be called during life cycle depending on some conditions. Depending on your requirement, you may have to implement these methods in your application for proper handling of state.
My application code using all these methods:
Login Activity:
output: ( Before pause)
output: ( After resume from pause)
Note that
onPostResume()
is invoked even though it's not quoted as life cycle method.I like this question and the answers to it, but so far there isn't coverage of less frequently used callbacks like onPostCreate() or onPostResume(). Steve Pomeroy has attempted a diagram including these and how they relate to Android's Fragment life cycle, at https://github.com/xxv/android-lifecycle. I revised Steve's large diagram to include only the Activity portion and formatted it for letter size one-page printout. I've posted it as a text PDF at https://github.com/code-read/android-lifecycle/blob/master/AndroidActivityLifecycle1.pdf and below is its image:
ANDROID LIFE-CYCLE
There are seven methods that manage the life cycle of an Android application:
Answer for what are all these methods for:
Let us take a simple scenario where knowing in what order these methods are called will help us give a clarity why they are used.
onCreate()
- - - >onStart()
- - - >onResume()
onPause()
- - - >onStop()
onRestart()
- - - >onStart()
- - - >onResume()
onStop()
- - - >onDestroy()
There are four states an activity can possibly exist:
Starting state involves:
Creating a new Linux process, allocating new memory for the new UI objects, and setting up the whole screen. So most of the work is involved here.
Running state involves:
It is the activity (state) that is currently on the screen. This state alone handles things such as typing on the screen, and touching & clicking buttons.
Paused state involves:
When an activity is not in the foreground and instead it is in the background, then the activity is said to be in paused state.
Stopped state involves:
A stopped activity can only be bought into foreground by restarting it and also it can be destroyed at any point in time.
The activity manager handles all these states in such a way that the user experience and performance is always at its best even in scenarios where the new activity is added to the existing activities
Activity has six states
Activity lifecycle has seven methods
onCreate()
onStart()
onResume()
onPause()
onStop()
onRestart()
onDestroy()
Situations
When open the app
When back button pressed and exit the app
When home button pressed
After pressed home button when again open app from recent task list or clicked on icon
When open app another app from notification bar or open settings
Back button pressed from another app or settings then used can see our app
When any dialog open on screen
After dismiss the dialog or back button from dialog
Any phone is ringing and user in the app
When user pressed phone's answer button
After call end
When phone screen off
When screen is turned back on
From the Android Developers page,
onPause():
onStop():
Now suppose there are three Activities and you go from A to B, then onPause of A will be called now from B to C, then onPause of B and onStop of A will be called.
The paused Activity gets a Resume and Stopped gets Restarted.
When you call
this.finish()
, onPause-onStop-onDestroy will be called. The main thing to remember is: paused Activities get Stopped and a Stopped activity gets Destroyed whenever Android requires memory for other operations.I hope it's clear enough.