Learn about Android Activity and its lifecycle in this easy-to-understand guide. Discover the key stages of an Activity’s lifecycle, from creation to destruction, with real-world examples.
What is an Activity in Android?
An Activity in Android is essentially a single screen in an app. Each time you open an app, what you see—whether it’s a home screen, login page, or settings page—is called an Activity. Think of it as a room in your house. Each screen or page of an Android app is like a different room, and every Activity does something specific.
If you open a messaging app and go from the chat list to a specific conversation, each of these screens represents a different Activity. When you interact with buttons, input text, or view information, you’re working within that particular Activity.
The Android Activity Lifecycle Explained
Activities don’t stay the same throughout their lifetime. Android manages them using an Activity Lifecycle, which determines when they start, pause, or stop. The system needs to manage memory efficiently, and this lifecycle helps with that.
Here are the key stages:
1. onCreate() – The Activity is Created
When an Activity is launched for the first time, Android calls the onCreate()
method. This is the beginning of the lifecycle. Developers use this phase to set up essential components such as user interfaces, buttons, or text fields. Think of it as waking up in the morning and getting ready for the day. The system sets the foundation for the Activity to function.
2. onStart() – The Activity is Becoming Visible
After creation, the onStart()
method runs, making the Activity visible to the user. At this stage, the Activity is not yet fully active but can be seen. Imagine you’re ready for work but haven’t started any tasks yet. Similarly, the Activity is visible but isn’t doing anything interactive.
3. onResume() – The Activity is Active
When the user can start interacting with the Activity, Android calls onResume()
. This is when the app is in focus and ready for interaction—like when you start working at your desk. The system brings the Activity to the front, allowing users to interact with buttons, type, or scroll through content.
4. onPause() – The Activity is Paused
If you switch to another app or a new screen, Android calls onPause()
. In this phase, the Activity is still alive but no longer in focus. It’s like stepping away from your desk for a break. The Activity is paused but can come back to the foreground without starting from scratch.
5. onStop() – The Activity is Hidden
When the user navigates away or closes the app, the system calls onStop()
. This means the Activity is no longer visible. Think of this as leaving your desk and going home. The Activity is still in memory, but it’s not visible on the screen.
6. onRestart() – The Activity is Restarted
If the user returns to the app after it has been stopped, onRestart()
is called. This method prepares the Activity to come back to the foreground. It’s like picking up where you left off. The app doesn’t need to go through the whole creation process again.
7. onDestroy() – The Activity is Destroyed
Finally, when an Activity is closed entirely or the system needs to free up resources, onDestroy()
is called. The app releases all its resources, and the Activity is removed from memory. It’s like finishing your work and going to bed—everything shuts down.
Real-Life Example of the Activity Lifecycle
Imagine you’re using a messaging app:
- onCreate(): The app opens, and you see the chat list.
- onStart(): The chat screen becomes visible.
- onResume(): You start typing and send a message.
- onPause(): You switch to another app without closing the messaging app.
- onStop(): The app is completely hidden, but still running in the background.
- onRestart(): You return to the chat screen to continue your conversation.
- onDestroy(): You close the app, and it shuts down completely.
Understanding this lifecycle helps you see why your app behaves a certain way when you multitask, open multiple apps, or close them.
Why Does the Activity Lifecycle Matter?
Android needs to manage multiple apps running at the same time, and the Activity lifecycle ensures that the device runs efficiently. If developers don’t properly handle these stages, apps may crash or behave unexpectedly. For example, if the onPause()
and onStop()
stages aren’t managed, an app might lose your unsaved data when you switch to another app.
Want to learn more from the official Android documentation? Visit Android Developers for an in-depth look at the Activity lifecycle and best practices for app development.