Activities and Lifecycle
Android Development Fundamentals with Compose
Course 4 ยท Chapter 2 ยท When Android Creates and Destroys Your Screen
Your app does not decide when its screen starts, pauses or disappears: Android does, and tells your MainActivity through lifecycle callbacks. This matters in every app, because the
classic surprise is that rotating the phone destroys and rebuilds the Activity, and anything you kept in an ordinary variable is gone. This chapter lists the callbacks, shows you how to watch them in Logcat,
and shows the Compose tool for keeping a value across a rotation.
isFinishing, the Log.d pattern from a Compose codelab, and rememberSaveable as the way to survive a configuration change.
Checked on the install: the Logcat filter box and its query form (Android Studio 2: The New UI 7). General knowledge, not checked: what each callback is for in practice, and exactly what remember loses. All code is unrun (no Kotlin compiler on the machine used to write this).
Back-button and Home-button behaviour changes between Android versions, so the exercises ask you to record what your device does.
The Six Callbacks
(docs) An Activity has six core callbacks, called as it enters each new state:
| Callback | Usual meaning (general knowledge) |
|---|---|
onCreate | The Activity is being created. Set up the UI here: setContent goes in this one. |
onStart | The screen is becoming visible. |
onResume | The screen is in the foreground and the user can interact with it. |
onPause | The user is leaving, or another window is covering part of the screen. |
onStop | The screen is no longer visible. |
onDestroy | The Activity is about to be destroyed. |
Each override calls super first, so the base class still does its work. Compose code usually does not override these callbacks itself for every screen; a composable that needs to react to them uses the Lifecycle
API (the docs mention LocalLifecycleOwner.current for adding an observer), which a later chapter can show.
Watching Them in Logcat
(docs) A Compose codelab teaches this pattern: declare a TAG constant and call Log.d(TAG, "...") in each override.
Run the app, then in Logcat (Android Studio 2: The New UI 7) filter with package:mine & tag:MainActivity. You should see onCreate, onStart and onResume as the app opens.
Rotation Destroys the Activity
(docs) When the device rotates, the original Activity receives onStop and onDestroy, then a new instance is created and receives onCreate, onStart and onResume.
The docs add that onDestroy runs either because the Activity is finishing (something called finish()) or because the system is destroying it temporarily; isFinishing tells you which, which is why the listing logs it.
A new instance means new variables: anything held only in memory by the old one is gone.
Keeping a Value: remember and rememberSaveable
(docs) The codelab says to declare a value with rememberSaveable if it must survive a configuration change. (general knowledge) A plain remember keeps a value while the composable stays on screen,
but not across the Activity being recreated. This small composable shows both. Chapter 4 explains remember and state properly; for now just compare what happens on rotation.
Tap each button a few times, rotate the device and watch which number resets.
Compared with the XML Way
| Views (Android Development Fundamentals) | Compose | |
|---|---|---|
| Where the UI is built | onCreate, with setContentView | onCreate, with setContent |
| Saving a value across rotation | onSaveInstanceState and the savedInstanceState bundle | rememberSaveable |
| Reacting to lifecycle events | Override the callbacks | Override them, or observe them with the Lifecycle API |
| Saved data survives | Rotation and being killed in the background | The same (general knowledge) |
savedInstanceState. The callbacks are unchanged; what changes is how you keep state (rememberSaveable) and how you read the log (the New UI's query filter, Android Studio 2: The New UI 7).
Try It Yourself
Add the logging overrides to MainActivity, run the app and write down the callbacks you see in Logcat as it opens. Then rotate the device and write down the new sequence.
Add the Counter composable to setContent. Tap each button three times, rotate the device, and say which value was kept and which was lost.
With logging in place, record the callbacks (and the finishing value) when you (a) press Back, (b) press Home and return, (c) rotate. Say which of them show finishing = true.
isFinishing; the TAG and Log.d pattern; rememberSaveable for values that must survive a configuration change; LocalLifecycleOwner.current for observing the lifecycle in Compose.Checked on the install: the Logcat query filter and the
package:mine form.Not verified: the "usual meaning" column and the exact limits of
remember (general knowledge); what rememberSaveable can store beyond simple values; the callback order for Back and Home on your Android version; every listing and solution (unrun); the exercises (not run by me).Sources: The activity lifecycle (developer.android.com), Stages of the Activity lifecycle (developer.android.com codelab), Activity state changes (developer.android.com).
Row, Column, Box, Modifier and the Material 3 components.