Android Lifecycle Callbacks
As an Android developer, one of the most common questions I encounter in interviews is about the Activity Lifecycle. I wanted to discuss it using a messaging app example. Hoping it makes the explanation more understandable and memorable.
Before we delve into the Activity Lifecycle, let’s answer the question: What is an Activity?
What is an Activity?
In Android, an Activity represents the functionality that the application provides on a single screen. Each activity has a window that can interact with the user and is one of the fundamental building blocks of Android applications. Activities manage the different tasks, screens, or user interfaces of applications. When a user navigates from one screen to another within an application, they might actually be transitioning from one activity to another.
Here, I would like to open a parenthesis on the subject of Activities with Jetpack Compose.
With Jetpack Compose, unlike traditional XML layouts, usually, a single
Activityis used, and all UI in this Activity is defined usingcomposablefunctions. This structure eliminates the need to create multiple activities to manage different screens of the application and manages all screen switching operations through components like NavHost and NavController. These components dynamically change which content is displayed in the user interface. This simplifies the application architecture and can improve performance.
What are the Activity Lifecycle methods?
In Android, every Activity has a specific lifecycle that defines transitions between various states of an Activity and the actions that can be performed in these states. The Android operating system provides various callback methods to manage the states of Activities. These methods are called automatically, starting from the creation of the Activity, until it is viewed by the user, paused and finally destroyed.
An example messaging application might use these methods in a variety of ways to improve user experience, ensure data security, and optimize resource management. Here are the potential uses of these methods in a messaging application:
Get GokcenYener’s stories in your inbox
Join Medium for free to get updates from this writer.
onCreate(): When the application is launched, the onCreate() method loads the list of chats to be displayed on the main screen. Here, user interface elements are initialized, and the latest states of chats are retrieved from the disk or a local database. Additionally, all necessary permissions are checked and requested at this stage.
onStart(): When the user opens the app and it becomes visible, the onStart() method checks for new messages or updates from the server. This ensures that the application always presents the most up-to-date information to the user.
onResume(): If the user switches to another app and then returns to the messaging app, the onResume() method checks for new notifications received in the background and updates the interface. Also, when the app becomes active again, it may re-build WebSocket connections for instant messaging.
onPause(): When the user leaves the app, the onPause() method can activate security measures such as screenshot protection for sensitive information. Additionally, real-time processes like live audio or video calls can be paused or the user can be notified.
onStop(): When the app is completely hidden from the user, the onStop() method can temporarily stop some background services to reduce power consumption. For example, features that require continuous data updates, like location sharing, can be deactivated.
onRestart(): When the app is restarted, the onRestart() method reverses any stop or pause operations, restoring the app to its previous active state. This may include resuming interrupted chats or calls.
onDestroy(): When the app is completely closed or terminated due to an error, the onDestroy() method releases all resources and closes any open database connections. Additionally, all session information is securely cleared to ensure a clean start on the next launch of the app.
Using each lifecycle method in this way helps optimize both the performance and the user experience of the application. Especially for a messaging app, real-time data updates and resource management are critically important.
I hope you liked my article. If you have any questions or feedback, please feel free to contact me.








