Android Activity Lifecycle Explained
In this article
An Android Activity is a single, focused user interaction screen, such as a login page or settings menu, and its lifecycle consists of a series of callback methods that manage its state transitions—from creation to destruction—as the user navigates between apps and screens. These lifecycle methods allow developers to allocate and release resources efficiently, preserve user data during interruptions like phone calls, and ensure smooth navigation.
What Is an Android Activity?
In Android development, "Activity" is a standard technical term, not informal or slang. It refers to a core component of an Android app that provides a visible user interface (UI) screen. Each Activity is a subclass of the android.app.Activity class and typically corresponds to one screen in an app. For example, an email app might have separate Activities for the inbox list, composing a message, and viewing a single email.
The term "Activity" was chosen by Google’s Android team to reflect the idea of user engagement or an active task. It is used in official documentation, code, and developer tools. In a sentence: "When the user taps the notification, a new Activity is launched to display the message."
How the Activity Lifecycle Works
The Activity lifecycle is a defined sequence of states and callback methods that Android invokes as an Activity moves between the foreground, background, and termination. These callbacks allow developers to manage app behavior during interruptions (like incoming calls), configuration changes (like screen rotation), and memory pressure.
The lifecycle consists of seven primary callback methods:
- onCreate(): Called when the Activity is first created. Used to initialize the UI and bind data.
- onStart(): Called when the Activity becomes visible to the user.
- onResume(): Called when the Activity starts interacting with the user. The app is now in the foreground.
- onPause(): Called when the Activity loses focus but is still partially visible (e.g., a dialog appears).
- onStop(): Called when the Activity is no longer visible to the user.
- onRestart(): Called when the Activity is restarting after being stopped.
- onDestroy(): Called before the Activity is destroyed. Used for final cleanup.
These methods are defined in the Activity class and must be overridden in your subclass to add custom behavior.
Visualizing Lifecycle State Transitions
The lifecycle can be visualized as a state machine:
- App launch:
onCreate()→onStart()→onResume() - User navigates to another app:
onPause()→onStop() - User returns:
onRestart()→onStart()→onResume() - Activity is closed:
onPause()→onStop()→onDestroy()
Note: If a dialog appears over the Activity, only onPause() is called—onStop() is not, because the Activity remains partially visible. This is a common source of confusion. Developers should avoid heavy operations in onPause() since it must execute quickly.
Best Practices for Lifecycle Management
Proper use of lifecycle callbacks prevents crashes, memory leaks, and data loss. Key practices include:
- Perform UI setup in
onCreate()—inflate layouts, find views by ID. - Start animations, sensors, or camera in
onResume(). - Pause or stop non-UI components in
onPause()—e.g., stop location updates. - Release resources in
onDestroy()—close database connections, unregister listeners. - Never perform long-running tasks in
onPause()oronStop()—these methods must return quickly.
A common mistake is registering a broadcast receiver in onCreate() but not unregistering it in onDestroy(), causing a memory leak. Always pair registrations with unregistrations in matching lifecycle methods.
Handling Configuration Changes
Configuration changes—like screen rotation, language switch, or keyboard attachment—can destroy and recreate the Activity by default. This triggers: onPause() → onStop() → onDestroy(), then a new onCreate().
To preserve data during such changes:
- Use
ViewModelto store UI-related data that survives recreation. - Use
onSaveInstanceState()to save transient UI state (e.g., scroll position). - Alternatively, declare
android:configChangesin the manifest to handle changes manually (not recommended for rotation).
Example: If a user is filling a form and rotates the screen, ViewModel retains the entered text, while onSaveInstanceState(Bundle) can save the current focus or checkbox state.
Common Lifecycle Problems and Fixes
Developers often encounter issues due to incorrect lifecycle usage:
- Crash on rotation: Caused by not preserving object references. Fix: Use
ViewModeloronSaveInstanceState(). - Memory leaks: Holding context references (e.g., from a static variable). Fix: Avoid storing Activity context in long-lived objects.
- Background work in onPause(): Can delay navigation. Fix: Move heavy tasks to
onStop()or use background services. - Double launches: Pressing a button that starts an Activity twice. Fix: Use
launchModeor check intent flags.
Always test lifecycle behavior by rotating the device, switching apps, and using the Recent Apps screen.
Frequently Asked Questions
What is the difference between onPause() and onStop()?
onPause() is called when the Activity loses focus but is still partially visible (e.g., a dialog is open). onStop() is called when the Activity is no longer visible at all (e.g., user switched to another app). onPause() always precedes onStop(), but an Activity can return from onPause() without going to onStop() if it regains focus immediately.
When should I use onDestroy()?
Use onDestroy() for final cleanup: unregister broadcast receivers, close database connections, and release any resources tied to the Activity. However, do not rely on it being called in all cases—Android may kill the process without calling onDestroy(). For critical cleanup, use onStop() or onPause() where appropriate.
Does onCreate() run every time the app is opened?
No. onCreate() runs only when the Activity is first created. If the user leaves and returns, onRestart() → onStart() → onResume() are called instead. However, if the system kills the app to free memory, onCreate() will run again when the user returns, restoring state from onSaveInstanceState() if available.
Can an Activity exist without a UI?
Technically, yes—but it’s not standard practice. Activities are designed for UI interaction. If you need background functionality without a UI, use a Service or WorkManager. Some legacy apps used UI-less Activities for tasks, but this is discouraged in modern Android development.
Why is onPause() called before onStop()?
onPause() is called first to give the Activity a chance to commit unsaved changes and stop ongoing actions (like animations) before it becomes invisible. It’s a lightweight method meant to execute quickly. onStop() follows when the Activity is fully hidden, allowing more expensive cleanup operations.
How do fragments affect the Activity lifecycle?
Fragments have their own lifecycle, but it is tied to the host Activity. For example, when an Activity’s onResume() is called, each of its active Fragments also receives onResume(). Fragment lifecycle callbacks are called just before or after their host Activity’s corresponding callbacks. Managing state across both requires coordination, often using ViewModel scoped to the Activity.