Thuta Learning
IntermediateMobile Developmentintermediate

Activity & Composable Lifecycle

Relax. We'll talk through this in plain words — no textbook voice.

What you'll walk away with

  • Understand Activity & Composable Lifecycle without the intimidation
  • Get comfortable running Android Studio/Compose code yourself
  • Apply this concept in a real project right away

Let's think about it this way for a second

Each Activity (each screen) has a lifecycle state (`onCreate`, `onStart`, `onResume`, `onPause`, `onStop`, `onDestroy`) — sending the app to the background triggers `onPause`/`onStop`, and reopening it triggers `onResume`. Rotating the screen (portrait → landscape) destroys and recreates the Activity by default — state held in `remember { mutableStateOf(...) }` can get wiped out on rotation (it doesn't survive configuration changes). Using `rememberSaveable` keeps that state intact even through a rotation.

Let's connect this to a real scenario

If the Counter app (Intermediate lesson 2) is written with `remember`, rotating the screen can reset the count value back to 0 — rewriting it as `rememberSaveable { mutableStateOf(0) }` keeps the count value intact through a rotation.

Let's walk through it together

kotlin
@Composable
fun Counter() {
    // Survives rotation — remember alone would not
    var count by rememberSaveable { mutableStateOf(0) }

    Column {
        Text("Count: $count")
        Button(onClick = { count++ }) { Text("Increment") }
    }
}
You should see
Even after rotating the emulator (Ctrl+F11), you should see the count value stick around instead of disappearing.

5-minute try-it

Run the Counter written with `remember`, click count 3 times, then rotate the emulator (Ctrl+F11) — you should see the value disappear. Then switch to `rememberSaveable` and try again — this time the value should stay put.

A quick word of caution

`rememberSaveable` can't save just any object — it only works for primitive types (Int, String) and objects that implement `Parcelable`/`Serializable`.

Easy traps

  • Holding important user input state (form data) with just `remember` instead of Saveable — a rotation can wipe out data the user already typed in
  • Running a heavy operation (a large file write) inside a lifecycle callback like `onPause` — this can block the UI thread

Now try it yourself

Run the Counter written with `remember`, click count 3 times, then rotate the emulator (Ctrl+F11) — you should see the value disappear. Then switch to `rememberSaveable` and try again — this time the value should stay put.

You'll know it worked when: Even after rotating the emulator (Ctrl+F11), you should see the count value stick around instead of disappearing.

Activity & Composable Lifecycle | Thuta Learning