Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Activities and Their Lifecycle  ›  Lesson

The Activity Lifecycle

Activity LifeCycle 15 minAn activity moves through created, started, resumed, paused, stopped and destroyed states, and the system calls a matching callback at each step.

The Activity Lifecycle

Sprout's detail screen shows a live countdown to the next watering. If the user switches to another app, that countdown should stop wasting battery, and when they return it should pick up again. To do that you need to know exactly when the system tells your activity it is visible, in front, or gone.

The rule

Up: create, start, resume. Down: pause, stop, destroy.

Each callback marks a change of state:

Callback What just happened
onCreate The activity exists. Set up the UI once.
onStart It is visible.
onResume It is in the foreground and receives input.
onPause It is losing the foreground (maybe partly covered).
onStop It is no longer visible.
onDestroy It is being removed.

When a stopped activity comes back, the system calls onRestart, then onStart and onResume again. onCreate does not repeat.

Pair your work with mirror callbacks: what you start in onStart, stop in onStop; what you start in onResume, stop in onPause.

override fun onStart() { super.onStart(); countdown.start() }
override fun onStop()  { countdown.stop(); super.onStop() }

The shortcut

Picture a staircase: create → start → resume on the way up, pause → stop → destroy on the way down. Any user action just walks some steps.

Home, then back to SproutStep 1 of 4
  1. Open the detail screen

    onCreate → onStart → onResume. The countdown starts in onStart.

The common trap: doing heavy work in onPause. It must finish quickly, because the next screen waits for it. Save lightweight state there and keep slower cleanup for onStop.

Drills

Answer fast — trust the rule.

quick drill

In what order are callbacks called when an activity first appears?

quick drill

After which callback is the activity no longer visible?

quick drill

The user presses Home, then reopens Sprout. Which callback is NOT called on the way back?

quick drill

You start a location listener in onResume. Where should you stop it?

Remember

  • Up: create, start, resume. Down: pause, stop, destroy.
  • Returning from stopped: onRestart → onStart → onResume, never onCreate.
  • Start and stop work in mirror pairs; keep onPause fast.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainWhere does the timer go?

Sprout's detail screen runs a live countdown to the next watering. Explain in which callbacks you would start and stop the countdown, and trace what happens when the user presses Home and later comes back.