Activity Lifecycle in Android
Table of Content
Activity lifecycle Diagram Android
[table id=5 /]
With every button you press and every option you choose on your smart phone, a number of processes take place behind the screen of your device. It might just be a little swipe for you, but it brings about a huge change in the app or the device you are using. These changes are highly systematic and carefully planned in order to give you—the user—a smooth experience.
All this might sound very interesting to a layman, but it is the key to making an efficient app for an Android app developer. Whether you are a beginner, an app developer or just someone reading this out of curiosity, this article will prove to be greatly informative for you. In this article, we will give you a detailed explanation that will cover each and every important point a person needs to know about Activity Lifecycle in Android. For this reason, we will have a look at:
Android is not just any operating system; it is an open source operating system. This means that the original source code of Android is freely available and can be used for viewing and modification. The Android operating system is based on Linux with a Java programming interface, and this operating system is for mobile devices, such as smart phones, tablets, etc.
The Android operating system consists of multiple APIs (Application Program Interfaces) so that it can support services that are based on location, like GPS. In addition to that, the operating system supports multimedia hardware control for playback and recording. Moreover, it can multitask so that a user can change task windows and open multiple applications (or apps) at a time. This allows the user to reuse the app components and replace native apps.
Activity in Android
An Activity in Android is always in front of the user. If you open an app in Android right now, some UI (user interface) elements will appear on your screen. This new window or screen is called an Activity in Android. Whenever you open an app, you always initiate an Activity in the operating system. Being such a recurring thing makes Activity a basic component of Android.
A real life example of an Activity in Android can be taken from the Gmail application. Consider that you have opened your Gmail app. The first thing you would see is your inbox, showing the emails you have received. Those emails appearing on your screen are part of an Activity. If you click and open one of your emails, that email would be opened in another Activity.
In better words, an Activity in Android is a class that is used to provide a window where UI elements of an app are drawn. The first screen or Activity that a user interacts with right after opening an app is usually referred to as MainActivity. Any Activity can be called from any Activity.
When a developer starts coding for his project, he knows about the main method from which the program initiates execution. In the same manner, Android apps initiate their process from an Activity. As mentioned above, an Activity is one screen of the UI of an app. Also, we see many screens in an app, one after the other. This means that there is a lifecycle associated with each Activity of an app. To form a code free of errors and create a well-developed app, an Android app developer must understand the lifecycle of an Activity.
Activity Lifecycle in Android
Android apps work differently than typical apps that we see on laptops. When compared with a laptop, the resources in a smart phone are limited (smaller screen, less memory, shorter battery life, etc.). Due to these limitations, a smart phone cannot easily display more than one app simultaneously. Therefore, in a smart phone, only one app is in the foreground at a given time.
This is not so bad because smart phones were originally made to be used on the go, and, in such a situation, a user can only focus on one app at a time. That being said, other apps can be opened by pushing the current app to the back. When the user is done working on the newly-opened second app, he can close it to bring the first app back in the foreground. This whole mechanism is the same for an Activity in Android.
Therefore, an app or Activity can go through many possible states, and the set of all these possible states is collectively called an Activity Lifecycle.
Activity Lifecycle and the Activity Stack
As you navigate through an Android app, different stages of an Activity lifecycle come in action. Basically, Activities in Android are seen and treated like an Activity “Stack.” To understand this concept better, picture yourself opening an Activity. In an Activity Stack, this newly-opened Activity would be placed on the top of the stack. When an Activity is on top of the stack, it means that it is the currently running Activity.
Now, picture yourself opening a second Activity. This second Activity will now replace the first Activity in the Activity Stack, moving to the top of the stack. For this reason, the first Activity will go below the second and will not come to the top again until the second Activity is running.
The Activity class comes with a number of callbacks. These callbacks enable the Activity to learn that a state has been changed. For instance, these changes inform the Activity that the system it inhabits is being created, resumed, stopped, etc.
States of an Activity Lifecycle
The Activity Lifecycle comprises various states, or we can say that an Activity goes through various states in its lifecycle. Note: You must be careful and not mix these states up with the methods involved in an Activity Lifecycle. To make it easier for you, the states of an Activity Lifestyle are governed by those methods. An Activity Lifecycle has six interlinked states:
1. When an Activity is in the foreground of a screen (or at the top of the stack), it is said to be in the running or resumed state. To achieve the foreground, the activity has to be created, started and then sent to the resumed state. This is the activity that the user is currently interacting with, and the Android operating system does not recycle such an activity under normal circumstances.
2. In some cases, the activity on the top of the stack does not occupy the whole screen. Therefore, if an activity is not on the top spot anymore but is still visible, it is said to be in the paused state. However, the Android system would kill this paused activity, to release the resources, if the memory is low. Also, when an activity opens a second activity, the first activity achieves the paused state. In both the scenarios, when the user goes back to the first activity, the first activity returns to the resumed state.
3. When an activity is being sent to a lower spot of the stack to be completely invisible, it goes through the paused state and then to the stopped state. In the stopped state, the activity still retains all the information. If a stopped activity is opened again, it is first started and then sent to the resumed state. However, if the system is running low on memory, it would kill the stopped activity to free up the resources.
4. When the Android system is removing the activity from the stack (by either asking it to finish or simply killing it), the activity is said to be in the destroyed state. If the user wishes to display a destroyed activity, he must restart it completely to restore it to its previous state.
States of an Activity Lifestyle and Their Visibility
While learning about Activity Lifestyle and its states, one can find it difficult to visualize the states in his mind. Not only that, it is significantly tough for a beginner to understand which state is even visible and which state is not. So here are some points to eliminate those confusions:
- In the created state, the activity is not yet visible
- In the started state, the activity is visible
- In the resumed state, the activity is visible
- In the paused state, the activity is partially visible
- In the stopped state, the activity is hidden
- In the destroyed state, the activity is invisible as it has gone from the memory
This was all an Android app developer needs to know about Activity Lifecycle in Android before starting to code the Activity Lifecycle into his project. The article should make it clear to all readers that Activity Lifecycle is one of the most important aspects of Android, and, thus, it should not be ignored while developing an app. Without the Activity Lifecycle in Android, an app would have no systematic way of functioning whatsoever. In fact, it is also possible for an app to not even start without the Activity Lifecycle. Therefore, the Activity Lifecycle must be taken very seriously by beginners and, especially, Android app developers.