What is the life cycle of an Android activity? Why are so many similar sounding methods (onCreate()
, onStart()
, onResume()
) called dur
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:
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()
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