Android from the foundation · Puzzle 19 · 20 of 20

When the process is gone

In puzzle 18, the ViewModel survived turning the phone, but it still lives in your process. Here's what's left when the process itself is ended, and where it waits.

The problem

In puzzle 18, the ViewModel survived turning the phone, because it waited in your process. In puzzle 12, you saw that when memory runs short, Android ends the processes of apps in the background. The user typed half a message, switched to another app for a minute, and came back. The process is gone, and with it every object it held, ViewModels included. Is the message lost?

See it

As the user leaves, your app sends a small Bundle with the draft to system_server by Binder; the process is ended to free memory; when the user comes back, a new process gets the Bundle back; after Back, system_server throws it away kernel Binder system_server your appViewModeldraft: "Hi Ana" process ended new processnew Activitydraft: "Hi Ana" Hi AnaBundle Hi Ana screen closedBack
  1. The user switches apps, so your Activity is stopped. The draft is a value your app chose to save (code below), so it's packed into a Bundle, a small map of simple values. Binder carries it to system_server: the screen's saved state.

    Step 1 of 4
  2. Memory runs short, and Android ends your process in the background: process death. The ViewModel and every other object go with it. Predict: the user comes back. Is the draft still there?

    Step 2 of 4
  3. Yes. A new process starts, and system_server hands the Bundle back by Binder. The new Activity reads the draft from it.

    Step 3 of 4
  4. But if the user closes the screen for good with Back, there's nothing to come back to, so system_server throws the saved state away.

    Step 4 of 4

The building block

When Android ends your process in the background to free memory, that's process death. Nothing inside the process survives it.

So, before it can happen, your app sends a little of its state somewhere that outlives the process: system_server. Each time your Activity is stopped (the user left it), your app packs what it chose to save into a Bundle, a small map of simple values like text and numbers, and Binder carries it to system_server. That copy is the screen's saved state. When the user comes back to a process that's gone, a new process starts (a cold start, puzzle 12), and system_server hands the Bundle back to the new Activity.

Saved state crosses Binder twice, through the receiver's buffer of about 1 MB (puzzle 16), and it's kept in system_server's memory. So keep it small: what the user typed, an ID, a scroll position. Big data, like the downloaded list, goes in a file on the phone and is read again by its ID.

An Activity fills its Bundle itself, in onSaveInstanceState(). A ViewModel can use saved state too, through a SavedStateHandle: a map that's kept in the saved state. viewModels() hands one to the ViewModel when it creates it.

class DraftViewModel(private val saved: SavedStateHandle) : ViewModel() {
    var draft: String
        get() = saved["draft"] ?: ""
        set(value) { saved["draft"] = value }
}

Saved state survives turning the phone too: the old Activity is stopped before it's destroyed, so its Bundle is saved and handed to the new Activity.

Saved state is for coming back. If the user closes the screen for good with Back, there's nothing to come back to, so system_server throws it away.

Kept inSurvives turning the phoneSurvives process deathSurvives Back
a field of the Activitynonono
a ViewModelyesnono
saved stateyesyesno
a file on the phoneyesyesyes

The analogy

Before the house can be torn down, the clerk mails the town hall a postcard with the few things that matter. If the house is rebuilt, the town hall mails the postcard to the new clerk. If the owner moves out for good, the town hall bins it.

Check yourself

The user typed a long message, kept only in a ViewModel. They switch apps, and Android ends your process to free memory. They come back. What do they see?

Right. Only what was in the saved state (or in a file) comes back.

Not quite. system_server keeps only the small Bundle your app sent, never your objects.

Not quite. A ViewModel lives in your process; when the process is gone, so is it.

Why must saved state be small?

Right. It crosses Binder, so it has to fit the shared ~1 MB buffer.

Not quite. A Bundle is a map and can hold many values; it's the shared ~1 MB buffer that limits it. Re-read that part ↑

Not quite. It waits in system_server's memory and crosses Binder; the Binder buffer is the limit. Re-read that part ↑

A ViewModel and saved state: which survives what?

Right. Different lifetimes: pick the place by what the data must survive.

Not quite. After Back there's nothing to come back to, so system_server throws it away.

Not quite. The ViewModel is an object in your process, so it ends with the process.

Sources

What you now know

Before your Activity is stopped, your app sends a small Bundle, its saved state, to system_server by Binder. If the process is ended in the background, a new process gets it back. Back throws it away. Keep it small: it crosses the ~1 MB Binder buffer.

That's everything published so far

You've reached the last puzzle of Android from the foundation. Next: Module 4, continued, about context and why it leaks, tasks and the back stack.

New puzzles are announced on LinkedIn and in the learning log feed.

All courses →