DEMO 01

Being an Activity

Press the buttons. Every one of them makes Android call a different method on the same object — and the log on the right is the proof.

What to do

Launch it. Let the clock run for a few seconds. Then press Home, wait, and come back — the seconds are still there. Now press Rotate. They are gone.

Then askWhy did Home keep the value, but rotation lose it?

Do not answer yet. Demo 02 fixes it, and it only lands if they have felt the loss first.

9:41 — no process —
The app has not been launched yet.
No activity object exists in memory.
◁ Back ○ Home
Logcat
  1. Nothing has run yet. Press “Launch the app”.
Lifecycle
Leave and come back
Partly covered
Done with it
Waiting Launch the app to begin. The strip above tracks four things at once: is it visible, is it interactive, does the object still exist, and does your timer value survive.

Six callbacks, three pairs

Do not make them memorise the ring. Three pairs carry the whole idea, and each pair answers one question about the activity.

Say this out loud“Born and dies. Visible or not. Interactive or not.”

onCreate() ⇄ onDestroy()

created or destroyed

Fires once per activity instance. This is the pair that decides whether the object exists at all.

onStart() ⇄ onStop()

visible or not

The user can see the screen, or cannot. The object is still alive during onStop() — just off screen.

onResume() ⇄ onPause()

interactive or not

Does the activity have the focus? A dialog on top takes the focus without hiding the activity.

Logcat — what one rotation actually prints
// the activity is destroyed and rebuilt, in this exact order
onPause()
onStop()
onDestroy()          // the old object is gone for good
onCreate(savedInstanceState = null)   // a brand new object
onStart()
onResume()
A mechanical stopwatch lying on a wooden panel

Photo Anton Makarenko · Pexels #1078057 · the Stopwatch app is the thread that runs through this whole lecture.

Because the activity never went through onStop() on its way down.

Rotation is a configuration change: Android takes the activity down to onDestroy() and builds a new one. onRestart() only fires when an existing, still-alive activity comes back from onStop() — which is exactly the Home-and-return case.

Run both paths in the simulator and compare the log. That difference — onRestart() present or absent — is the single fastest way to tell which of the two happened.