0Pricing
Android Academy · Lesson

The Android Lifecycle

Understand activity and composition lifecycles.

The Android Lifecycle is a free Android Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is the Lifecycle?

Every Android Activity moves through a series of states as the user opens, leaves, and returns to your app. This is the lifecycle.

Understanding it lets you start and stop work at the right times.

Core Activity Callbacks

The classic Activity lifecycle callbacks run in order:

  • onCreate — the Activity is being created.
  • onStart — becoming visible.
  • onResume — in the foreground, interactive.
  • onPause / onStop / onDestroy — leaving.

A Typical Flow

Opening an app: onCreateonStartonResume.

Pressing home: onPauseonStop.

Returning: onRestartonStartonResume.

Lifecycle States vs Events

The Jetpack Lifecycle library models this with States (CREATED, STARTED, RESUMED, DESTROYED) and Events (ON_CREATE, ON_START, ...).

Components can observe these instead of overriding raw callbacks.

LifecycleOwner

Activities, Fragments, and Compose hosts are LifecycleOwners. They expose a Lifecycle object that other components can observe.

val owner: LifecycleOwner = this // an Activity
val lifecycle = owner.lifecycle

Why This Matters for Resources

Many resources should only be active while the screen is visible:

  • Location updates.
  • Sensor listeners.
  • Camera sessions.

Start them in onStart/onResume and stop them in onStop/onPause to save battery and avoid leaks.

Lifecycle in Jetpack Compose

Composables do not have Activity callbacks. Instead they have a composition lifecycle: they enter the composition, recompose, and eventually leave it.

Reacting to Composition with DisposableEffect

DisposableEffect runs setup when a Composable enters the composition and cleanup when it leaves.

@Composable
fun Screen() {
    DisposableEffect(Unit) {
        // setup, e.g. register listener
        onDispose {
            // cleanup when leaving composition
        }
    }
}

Observing the Real Lifecycle in Compose

To react to actual Activity lifecycle events from a Composable, observe the LifecycleOwner with a DefaultLifecycleObserver.

@Composable
fun OnResume(onResume: () -> Unit) {
    val owner = LocalLifecycleOwner.current
    DisposableEffect(owner) {
        val obs = object : DefaultLifecycleObserver {
            override fun onResume(o: LifecycleOwner) = onResume()
        }
        owner.lifecycle.addObserver(obs)
        onDispose { owner.lifecycle.removeObserver(obs) }
    }
}

The ViewModel and the Lifecycle

Remember: the ViewModel outlives individual lifecycle states like onStart/onStop. It is cleared only when the owner is permanently gone. This is exactly why it is safe to hold state there.

Putting It Together

A well-structured screen:

  • Keeps durable state in a ViewModel.
  • Starts/stops visible-only resources with lifecycle awareness.
  • Collects flows only while the UI is at least STARTED (next lesson).

Quick Check

Test your lifecycle knowledge.

Recap

You learned:

  • Activities move through CREATED, STARTED, RESUMED, DESTROYED states.
  • Compose has its own composition lifecycle, handled via DisposableEffect.
  • Observe LifecycleOwner to react to real lifecycle events.
  • ViewModels deliberately outlive these states.

Frequently asked questions

Is the “The Android Lifecycle” lesson free?

Yes — the full text of “The Android Lifecycle” is free to read here on the web, and the Android Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Android Academy course, upgrade to CoddyKit PRO.

What will I learn in “The Android Lifecycle”?

Understand activity and composition lifecycles. You practise Android Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Android Academy?

No prior experience is required. Android Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “The Android Lifecycle” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Android Academy lesson?

Yes. Every Android Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Why Use a ViewModel?
  2. Exposing State from ViewModel
  3. The Android Lifecycle
  4. collectAsStateWithLifecycle
← Back to Android Academy