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.

What was checked, and how: read this first
Checked in the docs (one web search, summaries only): the six core callbacks, the order they run in on 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:

CallbackUsual meaning (general knowledge)
onCreateThe Activity is being created. Set up the UI here: setContent goes in this one.
onStartThe screen is becoming visible.
onResumeThe screen is in the foreground and the user can interact with it.
onPauseThe user is leaving, or another window is covering part of the screen.
onStopThe screen is no longer visible.
onDestroyThe 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.

// MainActivity.kt: log each lifecycle callback import android.util.Log private const val TAG = "MainActivity" class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) Log.d(TAG, "onCreate") setContent { /* your UI */ } } override fun onStart() { super.onStart() Log.d(TAG, "onStart") } override fun onResume() { super.onResume() Log.d(TAG, "onResume") } override fun onPause() { super.onPause() Log.d(TAG, "onPause") } override fun onStop() { super.onStop() Log.d(TAG, "onStop") } override fun onDestroy() { super.onDestroy() Log.d(TAG, "onDestroy, finishing = $isFinishing") } }

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.

import androidx.compose.foundation.layout.Column import androidx.compose.material3.Button import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableIntStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue
@Composable fun Counter() { // remember: the value is lost when the Activity is recreated (rotation) var plain by remember { mutableIntStateOf(0) } // rememberSaveable: the value survives rotation var saved by rememberSaveable { mutableIntStateOf(0) } Column { Button(onClick = { plain++ }) { Text("remember: $plain") } Button(onClick = { saved++ }) { Text("rememberSaveable: $saved") } } }

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 builtonCreate, with setContentViewonCreate, with setContent
Saving a value across rotationonSaveInstanceState and the savedInstanceState bundlerememberSaveable
Reacting to lifecycle eventsOverride the callbacksOverride them, or observe them with the Lifecycle API
Saved data survivesRotation and being killed in the backgroundThe same (general knowledge)
Compared with the Classic UI course
Android Development Fundamentals 2 teaches the same callbacks, Logcat and 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

Exercise 1

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.

๐Ÿ“„ View solution
Exercise 2

Add the Counter composable to setContent. Tap each button three times, rotate the device, and say which value was kept and which was lost.

๐Ÿ“„ View solution
Exercise 3

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.

๐Ÿ“„ View solution
Not Yet Verified
Checked in the docs (one web search, summaries only): the six callbacks and their order on rotation; 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).
Coming up
Chapter 3 covers composables and layouts: Row, Column, Box, Modifier and the Material 3 components.