Android Academy · Lesson

The Android Lifecycle

Understand activity and composition lifecycles.

Lesson 3 of 413 steps

The Android Lifecycle is a free Android Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, and your progress syncs across the web and the CoddyKit app. The Android Academy course includes 4 lessons in total.

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: onCreate → onStart → onResume.

Pressing home: onPause → onStop.

Returning: onRestart → onStart → onResume.

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.
Free to start

Learn Kotlin with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
36
Lessons
152

Frequently Asked Questions

Is the “The Android Lifecycle” lesson free?

Yes — any 3 lessons of the Android Academy learning path, including “The Android Lifecycle”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Android Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

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