Before I learned Android, I had doubts about how mobile applications work and maintain their state when some interruption occurs. Well, everyone has their doubts until they learn. I started with Activity and understood that it is possible because of the Android Activity lifecycle. In this blog, I will explain the concept and importance of the activity lifecycle.
So, what is an Activity? Well, Activity is just a java class in android with some predefined methods which are triggered at different app states. It’s a single screen in android, which should be inherited by the classes which have UI. Users interact with the screen to perform their tasks. As a user navigates through the app, the activity in the app goes through different stages in their life cycle.
The Android Lifecycle functions:
The activity has some predefined functions to handle the state changes in the application’s activity. Override the functions to achieve the required functionality when the activity’s state is being changed. It is not necessary that we implement all the callbacks. But it’s important to understand what they are used for, to ensure that the app works the way we want it to. The provided callback functions are,
The figure below shows the detailed flow of the callbacks called when the user is interacting with the application.
Let’s learn what these callback functions are in detail:
This is a callback function that should be implemented. The callback gets triggered as the activity enters the created state. The callback function is required as the basic setup for the application is done in the created state like, associate the activity with the ViewModel or to bind data to the UI, etc. The function also has a parameter savedInstanceState, a Bundle object which contains the previous state of the activity. If the activity never existed before, it simply returns a null.
The onStart() function is called when the application enters the started state. This function is called immediately after executing the onCreate() function. In this state, the activity comes to the foreground and becomes visible to the user. The activity will not stay in the started state for too long. Once the callback completes executing its task, it will move to the resumed state.
The onResume() function is called when the application enters the resumed state and comes to the foreground. In this state, the user interacts with the application. The activity stays in the same state until some action is performed on it. For example, users switching to another application or user receiving a phone call. The callback is called once the activity comes to a resumed state from a paused state. Here, we should get the components that are released in the paused state.
The onPause() function is called when the application enters the paused state by any action on the activity when it is in the resumed state. The callback is called as a result of activity going to the background(activity still may be visible to the user in multi-screen mode). Use this callback to handle things that should pause/stop when the activity is in a paused state. The application will not reside in this state for too long, so heavy load tasks shouldn’t be performed in this state.
The onStop() function is called when the application enters the stopped state. In this state, the activity is no longer visible to the user. This may happen for the following reasons, the user performing some action that invoked another activity to come to the foreground or the activity finished. In this state, the resources must be managed or released as the activity is no longer visible to the user. The activity either comes back to interact with the user after running the onRestart() or finishes by executing the onDestroy() callback.
The onRestart() function is called when the application comes back to the foreground from the background. This method is only called when the user navigates back to the stopped activity. The callback is immediately followed by the onStart() and onResumed() callbacks.
The onDestroy() function is called when the application enters the destroyed state. In this state, the activity gets killed, and the resources should be released that are not released in the onStop() state. The activity may enter this state because of two reasons,
- The activity is finishing due to the user dismissing the activity or called the finish() function.
- The system temporarily destroys the activity because of a configuration change.
If the onDestroy() is called due to user interaction or due to activity is finishing, onDestroy() is the final callback in the life cycle. If this callback is called due to a configuration change, then there is a chance that the activity will be created again by calling the onCreate() function. You can differentiate between the two by using the isFinishing() function. It returns a boolean, which indicates whether the activity is finishing or being dismissed temporarily. It returns true if the activity is finishing.
The callbacks onCreate() and onDestroy() are called only once in the lifecycle of an activity.
Let’s see some use cases of the activity lifecycle:
- The activity is created and is visible to the user: The functions called during this are
2. When the user presses the home button or switches between the applications. The functions called are,
The same callbacks are called when another activity comes to the foreground.
3. When the activity comes to the foreground from a stopped state (being in the background).
4. When the activity finishes either by calling finish() or due to configuration change. This state can be achieved by removing the application from the recent apps.
Why do we override these functions in Android Lifecycle?
Understanding and implementing these functions can avoid the App crashing when the user receives a phone call or moves to another app. These steps are useful to overcome the loss of the user’s progress when the user switches to another app or the device screen turns off. Additionally, to perform specific actions when the activity is at some stage, for example, getting the state of the user when the app resumes from the paused state or setting the initial configuration in the creating state.
I hope this blog is useful and clarified some of the basic concepts related to the android lifecycle. Please have a look at our extensive portfolio to view the snippets of my work. Thank you 😊