The lifecycle of an Activity in Android is the essential foundation for designing, programming, and optimizing mobile applications on this platform. Every screen you see in an Android app is an instance of an Activity , and how the user navigates, switches apps, rotates the device, or receives calls directly impacts the internal states of that Activity. Understanding this cycle is mandatory for any developer striving for robust, fast applications with a seamless user experience.
What is an Activity in Android and why is it so important?

An Activity It is more than just a graphical display: it represents the fundamental unit of interaction between user and application. From a technical point of view, an Activity is a Java/Kotlin class that extends Activity o AppCompatActivity and is declared in the file AndroidManifest.xmlEvery time the user opens a screen, performs an important action, or navigates between features, there's an Activity managing the resources, logic, and interface.
The Android system orchestrates Activities using a stack, ensuring that the user can return to previous screens using the back button. Android also automatically manages the lifecycle, freeing up resources when an Activity is no longer relevant, to optimize memory and battery life.

This means that there is no main() function like in other languages : Android decides which Activity starts first, when to stop, recreate or end them, depending on user interaction and the state of the device.
Why is it essential to master the Activity lifecycle?
Understanding and managing the lifecycle is key to the quality of any Android app:
- Prevents memory leaks: If you don't free up resources at the right time, you can run out of system memory and cause unexpected shutdowns.
- Prevents loss of user information: If you don't save the screen state when changing it, such as rotating it, the information may disappear, frustrating the user.
- Provides a seamless experience even with interruptions: When receiving a call, switching apps, or rotating the device, the app must react elegantly and quickly.
- Allows you to optimize resources (CPU, battery, sensors): Only active when strictly necessary.
- Facilitates navigation between screens (Intents, backstack): Actions and results must be managed according to the life cycle of each Activity.
Main states and callbacks of an Activity's lifecycle
An Activity's lifecycle is managed through callback methods that Android automatically invokes. Each one marks a specific point in the screen's life:
- onCreate(Bundle savedInstanceState): Initialization and creation of the interface. Here, the layout is inflated and basic resources are configured.
- onStart (): The Activity becomes visible but is not yet interactive.
- onResume (): The Activity takes center stage and is fully interactive.
- onPause (): The Activity loses focus but may still be partially visible (e.g., overlapping dialog).
- onStop (): The Activity is no longer visible, it remains in memory but does not consume active resources.
- onRestart (): Invoked if the Activity was stopped and returns to the foreground.
- onDestroy (): The Activity is about to be destroyed and must release all resources.
Detailed explanation of each method and best practices
onCreate (Bundle)
The first method that runs in the lifetime of an Activity . This is where the complete initialization takes place:
- The UI is inflated through
setContentView(R.layout.tu_layout) - Data is recovered from the
Intentthat started the Activity - Variables, ViewModels, adapters and listeners are initialized
- Data saved in is restored
savedInstanceStateif applicable
It runs only once per instance. All heavy initialization should go here, but it shouldn't block the main thread.
onStart ()
This marks the moment when the Activity becomes visible . The user can see it but not necessarily interact with it (it doesn't yet have focus). Perfect for non-critical UI update tasks or for launching visual resources.
onResume ()
The Activity takes focus; the user can tap, scroll, type, etc. Now's the time to:
- Reactivate sensors
- Resume paused animations or processes
- Update the UI based on external changes
Anything that requires the Activity to be visible and active must be started here, and stopped at onPause().
onPause ()
Android invokes it if the Activity is about to lose focus: when a dialog appears, another Activity is started on top of it, or the user navigates away.
- Pause animations, music, sensors, background tasks
- Stores ephemeral or ongoing data, but not persistent data
- Maintains the method very light; this is not the time to save critical information or perform heavy operations
onStop ()
The Activity is no longer visible. Here you should free up heavy resources, stop updates, close database connections, deregister listeners, and perform persistent saves.
It is executed just before onDestroy() if the Activity is to be destroyed, or before onRestart() if the user returns.
onRestart ()
It is only invoked if the Activity was stopped and returns to the foreground. It allows you to differentiate between a complete creation and a restart from memory.
onDestroy ()
Last call: Android is destroying the Activity instance. It frees resources, completes tasks, and saves essentials. Its execution isn't always guaranteed (if the system abruptly kills the process, it's not called).
Visual and conceptual states of an Activity
The cycle can be divided into conceptual states:
- Active (Resumed): In the foreground, interactive, and visible. Only one Activity can be this way per app.
- Paused: Partially visible, without focus. The user can't interact, but the Activity remains on screen (for example, if a dialog box appears).
- Stopped: It is not visible, but remains in memory for quick reaction if the user returns.
- Destroyed: Instance deleted. If the user returns, it is created from scratch unless the state has been saved.
- Launch (app opened for the first time):
onCreate()→onStart()→onResume() - Starting a new Activity:
onPause()→onStop()in the previous Activity,onCreate()→onStart()→onResume()in the new - Return to a stopped Activity:
onRestart()→onStart()→onResume() - Press Home or switch apps:
onPause()→onStop() - Rotate screen:
onPause()→onStop()→onDestroy()→ [new instance]onCreate()→onStart()→onResume()
These flows are common and should be implemented to avoid surprises in application behavior.
Configuration change management: rotation, language, resources
A configuration change (screen rotation, language change, dark mode, physical keyboard) causes Android to destroy and recreate the Activity to adapt to the new environment, which means losing the data stored in the instance's memory unless it is handled correctly. The mechanisms for preserving information are:
- onSaveInstanceState(Bundle): To save key-value pairs with the UI state (texts, positions, etc.) just before it is destroyed. They are recovered in
onCreate()oonRestoreInstanceState(). - ViewModel: Allows you to store UI-related data and state that must survive configuration changes. This is the modern and recommended solution.
- rememberSaveable (Jetpack Compose): Allows variables to resist recomposition and interface restarts.
For example, if the user completes a form and rotates the mobile device, onSaveInstanceState will save the entered fields and restore them after the screen is recreated.
Key differences: onSaveInstanceState, ViewModel, and rememberSaveable
- onSaveInstanceState(Bundle): For simple data (strings, numbers). Not suitable for large collections or complex objects, as it requires serialization/deserialization.
- ViewModel: Ideal for medium or complex data, as it lives as long as the Activity or Fragment exists (including recreations due to configuration changes).
- rememberSaveable: Specific to Jetpack Compose, it stores variables to resist recompositions and recreations of the Activity.
The choice depends on the type of data, architecture and technology (traditional UI vs Compose).
Managing Background Activities and Saving Resources
Android can pause, stop, or destroy hidden Activities to optimize memory and battery life . Background Activities are more likely to be terminated if the system needs resources. Therefore, you should:
- Release sensors, cameras, locations in
onPause()yonStop() - Save UI state before leaving the screen
- Avoid long-term tasks outside of a visible Activity
Activities communicate and navigate with each other using Intents , objects that describe the intention to perform an action:
- startActivity(Intent): Start an Activity without waiting for a result.
- startActivityForResult(Intent, int): Starts an Activity expecting it to return a result (useful for selecting a contact, taking a photo, etc.).
The result is received in onActivityResult() with a status code and the returned data.
Explicit and implicit intents: how to use them and differences
Intents can be :
- Explicit: They point to a specific class. They are generally used to navigate within the application.
- Implicit: They describe an action (for example, sharing or opening a link), allowing other applications to respond as to whether they can handle that action.
Examples:
- To open your own Activity:
Intent(this, ConfigActivity::class.java) - To share text by email:
Intent(Intent.ACTION_SEND)with typetext/plain
Patterns, best practices and common mistakes in the life cycle
- Don't block the UI thread en
onCreate(): Expensive operations (databases, network) should run in the background. - Avoid saving persistent data in onPause(): Better to use
onStop()or mechanisms such as ViewModel or databases. - Don't rely solely on onDestroy(): This method is not always invoked if the system kills the process abruptly.
- Use LiveData and Observers to keep the UI reactive to changes in data stored in the ViewModel.
- Super should always be called in callbacks: Call to
super.onCreate()and others to avoid breaking default system behaviors.
Correct task segmentation based on the cycle minimizes errors and maximizes app performance.
Examples of common use cases and lifecycle flows
- Open the app for the first time: Sequence
onCreate()→onStart()→onResume(). - Activity partially visible: A dialog box appears; it is executed only
onPause()but it is still visible. - Exit and return to the app: After pressing Home and back,
onRestart()→onStart()→onResume()(without returning toonCreate()). - Device rotation: The Activity is destroyed and recreated; it is executed again.
onCreate()afteronDestroy(). - Exit permanently (back button):
onPause()→onStop()→onDestroy()

These flows are commonplace, and developers must anticipate and test each scenario to avoid leaks, data loss, or inconsistent behavior.
Advanced Integration: Lifecycle, Processes, and Memory Management
Android can kill entire processes to free up memory, especially on low-resource devices. The degree of vulnerability that can be destroyed depends on the state:
| Status | Visible | In focus | Probability of being destroyed |
|---|---|---|---|
| Resumed (Active) | Yes | Yes | Low |
| Started/Paused | Yes | No | Media |
| Stopped | No | No | High |
| Destroyed | No | No | Total |
For this reason, the system never destroys an Activity individually: it kills the entire process, and with it all instances of Activities, Fragments, and services in that process.
How to save and restore the interface state correctly
To ensure that the user does not lose relevant information after events such as rotation, memory shutdown, or navigation, combine these approaches:
- Usa onSaveInstanceState(Bundle) for light and temporary data (text, selections, scrolling)
- Use ViewModel for complex and persistent data (lists, objects, network results)
- Uses databases or local storage for long-term or user information
Don't forget that to restore correctly, onCreate() you should check if the Bundle is null, and only read data if it contains valid values. onRestoreInstanceState() It is only invoked if something is saved. It always calls the methods super so as not to interrupt the restoration of the default view hierarchy.
Jetpack Compose and Lifecycle: Modern Integration
With Jet Pack Compose, the life cycle incorporates that of composables, whose state variables can survive recompositions (with remember) and configuration changes (rememberSaveable). Use:
- remember for variables that you only need during the current composition
- rememberSaveable for persistence in recreation of the Activity
- ViewModel for global or shared data across multiple screens or composables
Common mistakes in the Activity lifecycle and tips to avoid them
- Data loss after rotation: Not saving correctly in
onSaveInstanceState()or ViewModel. Solution: always save what is essential. - Memory leaks: Do not release listeners or close connections in
onStop()yonDestroy(). - UI stuck on return to foreground: Leaving heavy processes active after
onPause()oonStop(). Use threads or coroutines to separate expensive tasks. - Crash after returning to an Activity: Failure to initialize resources or failure to restore consistent state according to the cycle sequence.
Recommended resources to delve deeper into the Activities lifecycle
- Official Android Guide: Activity life cycle
- Hands-on Codelab: Stages of the activity life cycle
- Tutorials on GitHub about
ViewModeland advanced state management in Compose
Mastering the Activity lifecycle is the first step toward professional Android development. From declarations in the manifest to advanced state management and cross-screen communication, every detail counts. By properly utilizing callbacks, ViewModels, and Compose integration, you can ensure stable, fast, and ready-for-any-use applications, even on resource-constrained devices.
Table of Contents
- 1 What is an Activity in Android and why is it so important?
- 2 Why is it essential to master the Activity lifecycle?
- 3 Main states and callbacks of an Activity's lifecycle
- 4 Visual and conceptual states of an Activity
- 5 Common transitions: practical examples in navigation
- 6 Configuration change management: rotation, language, resources
- 7 Key differences: onSaveInstanceState, ViewModel, and rememberSaveable
- 8 Managing Background Activities and Saving Resources
- 9 Navigation and communication between Activities: Intents and results
- 10 Explicit and implicit intents: how to use them and differences
- 11 Patterns, best practices and common mistakes in the life cycle
- 12 Examples of common use cases and lifecycle flows
- 13 Advanced Integration: Lifecycle, Processes, and Memory Management
- 14 How to save and restore the interface state correctly
- 15 Jetpack Compose and Lifecycle: Modern Integration
- 16 Common mistakes in the Activity lifecycle and tips to avoid them
- 17 Recommended resources to delve deeper into the Activities lifecycle
