Android Activity Lifecycle
"Success is not final, failure is not fatal: it is the courage to continue that counts. "
If you find this article useful, Please add a comment and like our facebook page
In this post, I will explain the Android activity lifecycle and various event handlers associated with each stage changes. If you don’t know what an Activity in Android is, go to Android application components first.
We know how an application behaves in desktop platforms like Windows, Linux or Mac. After starting a program, we can minimize or restore it whenever we want and the software will preserve its state (unless the system crashes). The situation is entirely different in Android and we have lesser control on the lifetime of an application in Android. Therefore as a developer, we should make a thorough knowledge on Android activity life cycle before start developing.
An Activity in Android can exist in four states as described below:
1. Active/Running state
This is a state when an activity is in the front and has focus in it. It is completely visible and active to the user.
2. Paused state
In paused state, the activity is partially visible to the user but not active and lost focus. This occurs when some another Activity is on top of this one which doesnot cover the entire screen or having some transparancy so that the underlying Activity is partially visible. A paused activity is completely alive and maintains its state but it can be killed by system under low memory when memory can be freed by no other ways.
3. Stopped state
This is when the Activity is no longer visible in the screen. Another activity is on top of it and completely obscures its view. In this state also the activity is alive and preserves its state, but more likely to be killed by the system to free resources whenever necessary
4. Destroyed/Dead state
An Activity is said to be dead or destroyed when it no longer exists in the memory. Either the Activity hasn’t been started yet or once it was started and killed by the system in Paused or Stopped state to free resources.
The Android Activity Lifecycle Diagram
The figure below shows the Android Activity Lifecycle flow chart. Its very important for a developer, have a close study.
- When we launch an Activity in Android, it first calls the onCreate() method. This is where we do User Interface creation and initialization of data elements. This method is provided with a Bundle object as parameter to restore the UI state.
- onStart() method is called before the Activity is being visible to the User. Remember that Activity is still not Active.
- With the onResume() method, the Activity become visible and Active for the user to interact with. The Activity will be at the top of the Activity stack at this point. Now the Activity is in running /active state and is able to receive user inputs.
- In the Active state, onPause() method will be called when the system is about to resume another Activity on top of this one or when the user is about to navigate to some other other parts of the system. It is the last guaranteed call to a method before the Activity can get killed by the system. That is, there’s a possibility that your activity may be killed by the system at the paused state without executing any further method calls. Therefore it is important to save the user interface configuration and critical data at this method.
- By default, an Activity can remain in the paused state if:
- The user has pressed the home button
- Another activity or notification which is on top of it doesn’t completely obscures the visibility of underlying Activity.
- The device goes to sleep.
- There are three possibilities for an Activity under paused state:
- The user resumes the Activity by closing the new Activity or notification and the paused Activity gets Active/Running by calling onResume() method.
- It gets killed by the system under extremely low memory conditions. In this case there will be no further method calls before the destruction of the Activity and it needs to be re-run from the beginning by calling onCreate() and restoring the previous configuration from bundle object.
In all other cases it goes to stopped state by executing onStop() method. This is the default action when the user has pressed the back button, or a new activity which completely covers it resumes on top.
- An Activity under stopped state also has three different scenarios to happen:
- System kills it to free resources. An activity under stopped sate is more likely to be killed by the system than one in the paused state. It needs to start the cycle again withonCreate().
- It get restarted by calling onRestart() , onStart() and onResume() methods in the order if the user navigates back to the Activity again. In this case, the User Interface is intact and no need to be restored.
- onDestroy() method is called and the Activity is destroyed. This is the final method we can call before the Activity is destroyed. This occurs either because the Activity is finishing the operation or the system is temporarily destroying it to save space..
Android Activity Lifecycle Loops
By analyzing the Android Activity lifecycle diagram we can see there are three lifecycle loops exist for every Activity and are defined by those callback methods.
Entire Lifetime: This is the lifetime between the first call to the onCreate() and the final call toonDestroy() method. We create all global resources such as screen layout, global variables etc inonCreate() and release all resources with onDestroy() call.
Visible Lifetime: It is the lifetime of an Activity between onStart() and onStop() method calls. In this the Activity is visible to the user and he may or may not be able to interact with it. During the visible lifetime, an Activity maintains its state intact.
Foreground Lifetime: Foreground lifetime starts with onResume() and ends with onPause() method calls. During this, the Activity is completely visible to the user and is on top of all other Activities so that user can interact with it.
⇐Back to The Index of Android Development Tutorial
Stay tuned to EazyTutz for all future updates
Did you like this article about Android Activity Lifecycle? Add your comments and feedback below. I’d love to hear from you.