Android Activity Lifecycle: Complete State Machine & Memory

An Activity represents a single, focused user interaction surface with the screen in an Android application. As a user navigates between apps, receives phone calls, rotates the screen, or turns off the display, the Android operating system moves Activities through distinct lifecycle states via callback methods.

Android Activity Lifecycle State Machine

Figure 15.1: Complete Android Activity Lifecycle State Machine — Progression from Creation and Visibility to Foreground Execution, Pausing, and System Process Death.

The 7 Core Lifecycle Callbacks Explained

Lifecycle Callback Activity State Is Visible to User? Typical Operations Performed in Callback
onCreate() Created No One-time initialization: calling setContent {}, injecting Hilt dependencies, initializing ViewModels, and restoring savedInstanceState.
onStart() Started Yes (Visible) Initializes components needed while the app is visible (registering broadcast receivers, starting UI animations).
onResume() Resumed (Active) Yes (Has Focus) Activity is in the foreground and receiving user touch input. Resumes camera previews, starts audio, enables GPS updates.
onPause() Paused Partially Visible Another activity is in the foreground (e.g., a permission dialog or multi-window split screen). Pauses video playback, releases camera hardware.
onStop() Stopped No (Hidden) The activity is no longer visible on screen (user pressed Home or switched apps). Saves drafts, unregisters heavy listeners, pauses CPU-heavy work.
onRestart() Restarting Transitioning Called when an Activity in the Stopped state is brought back to the foreground before onStart() is invoked.
onDestroy() Destroyed No Final cleanup before the Activity is destroyed by calling finish() or because the OS reclaims memory during screen rotation.

Code Implementation: Modern ComponentActivity Lifecycle Logging

import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge

class MainActivity : ComponentActivity() {

    private val TAG = "ActivityLifecycle"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge() // Android 15 Edge-to-Edge default!
        Log.d(TAG, "onCreate: Activity created and state initialized")

        setContent {
            VeewomTheme {
                MainScreenScaffold()
            }
        }
    }

    override fun onStart() {
        super.onStart()
        Log.d(TAG, "onStart: Activity has become visible")
    }

    override fun onResume() {
        super.onResume()
        Log.d(TAG, "onResume: Activity has user focus (Foreground)")
    }

    override fun onPause() {
        super.onPause()
        Log.d(TAG, "onPause: Activity losing focus")
    }

    override fun onStop() {
        super.onStop()
        Log.d(TAG, "onStop: Activity is hidden from view")
    }

    override fun onRestart() {
        super.onRestart()
        Log.d(TAG, "onRestart: Returning from stopped state")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d(TAG, "onDestroy: Activity instance destroyed")
    }
}

Process Death & Low Memory Killer (LMK)

When the device runs low on RAM, the Linux Kernel's Low Memory Killer (LMK) driver kills background app processes. When the user later switches back to your killed app via the Recent Apps screen, Android creates a brand new process and calls onCreate(savedInstanceState). This is why business state must be preserved in ViewModels or persisted in Room / DataStore rather than kept in raw Activity member variables.

Frequently Asked Questions (FAQ) - Student & Interview Review

Q1. What happens during a screen rotation in terms of Activity lifecycle?

By default, rotating the screen is a configuration change that causes Android to completely destroy the current Activity (`onPause() -> onStop() -> onDestroy()`) and create a brand-new Activity instance (`onCreate() -> onStart() -> onResume()`) with updated layout resources.

Q2. What is the difference between onPause() and onStop()?

`onPause()` means the Activity has lost user focus but may still be partially visible (such as behind a dialog or in split-screen mode). `onStop()` means the Activity is completely hidden from the user's view (such as when the user presses Home).

Q3. Why shouldn't you perform heavy file I/O or network calls in onPause()?

`onPause()` must execute in under 50 milliseconds because Android will not transition to or render the next incoming activity until the current activity's `onPause()` method finishes executing. Heavy work in `onPause()` causes severe UI lag and ANR (Application Not Responding) warnings.

Explore Related Android Development Classes & Next Steps

Broaden your foundational and advanced Android application development skills across our curriculum:

Fragment Lifecycle & Navigation

Understand how fragment lifecycles mirror and attach to host activities.

ViewModel State Preservation

Retain complex state across screen rotations and configuration changes.

Explicit & Implicit Intents

Launch activities, pass Bundle extras, and parse result contracts.

Memory Leaks & LeakCanary

Avoid leaking Activity context in long-lived singletons and listeners.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Reddit

You may also like this!