download

Why does state survive rotation on Android, yet vanish after backgrounding?

Mobile~8 minReviewed 2 Dec 2026Free · No sign-in

Question

A photo-editor screen stores three pieces of state while you edit:

"KOTLIN"
// Inside EditActivity:
var zoomLevel = 2.5f

// Inside EditViewModel(private val state: SavedStateHandle):
val filter = MutableStateFlow("Warm")
fun cachePreview(bytes: ByteArray) { // 1,400 KB JPEG
    state["preview"] = bytes
}
  1. You rotate the phone. Which of the three values survive, and which reset?
  2. Next, you press Home and open two heavy games until Android kills your process. Why does state["preview"] crash with TransactionTooLargeException during backgrounding, and where should each piece live?
Show hints (3)
  1. "SPLIT TWO DEATHS"

    Screen rotation destroys only the Activity inside the same process. Background memory pressure kills the whole process and its RAM.

  2. "CHECK THE BINDER LIMIT"

    SavedStateHandle writes into a Bundle sent across Binder IPC. That shared process buffer caps out at 1 MB (1,024 KB).

  3. "MATCH SIZE TO STORE"

    Keep tiny UI values and keys in SavedStateHandle or rememberSaveable. Put large binary data or user work on disk.

Show checked explanation

Rotation resets zoomLevel as the Activity recreates, while filter and state["preview"] survive inside the same process (ViewModel and in-process saved state). Pressing Home sends the Bundle over Binder IPC to system_server, where 1,400 KB exceeds the 1 MB (1,024 KB) buffer and throws TransactionTooLargeException. Keep zoomLevel, filter, and a file URI in SavedStateHandle, and save image bytes on disk.

State-survival transitions and the 1,024 KB Binder buffer limit were checked in a Python 3.12 state model against Android 15 (API 35) and Jetpack Lifecycle 2.8 documentation. Checked by an editor other than the author.

Walkthrough

Android has three distinct lifecycle events, and each storage layer survives a different set:

storagerotationprocess deathuser closes screen
Activity field or Compose rememberlostlostlost
ViewModel in-memory propertykeptlostlost
SavedStateHandle / rememberSaveablekeptkeptlost
Room, DataStore, or app filekeptkeptkept

When you rotate the phone, Android recreates EditActivity, so zoomLevel resets to 2.5f. Both filter and state["preview"] survive rotation because the ViewModelStore and in-process ActivityClientRecord stay in process RAM.

Pressing Home changes the story. Android calls onSaveInstanceState and ships every saved Bundle (including SavedStateHandle and rememberSaveable entries) across Binder IPC to system_server. The Binder transaction buffer is fixed at 1 MB (1,024 KB) and shared across every active transaction in your process. A 1,400 KB JPEG immediately blows past 1,024 KB and throws TransactionTooLargeException. Android docs advise keeping total saved state under 50 KB.

If you store only small values and a file path in SavedStateHandle, both rotation and process death restore the screen cleanly:

"KOTLIN"
class EditViewModel(private val state: SavedStateHandle) : ViewModel() {
    val zoom = state.getStateFlow("zoom", 2.5f)
    val filter = state.getStateFlow("filter", "Warm")
    val previewUri = state.getStateFlow<String?>("preview_uri", null)

    fun onPreviewSaved(fileUri: String) {
        state["preview_uri"] = fileUri // small string, < 1 KB
    }
}

Write the 1,400 KB preview image into app storage or a Room-referenced cache file. After process death, EditViewModel reads zoom, filter, and previewUri from SavedStateHandle, then loads the bytes back from disk.

Common mistakes

  • Assuming ViewModel survives system-initiated process death. It lives in process RAM and vanishes when Android kills a backgrounded process.
  • Putting bitmaps, byte arrays, or full API lists in SavedStateHandle or Navigation arguments, overflowing the shared 1 MB Binder buffer.
  • Using Compose remember instead of rememberSaveable for small UI selections you expect to survive rotation and process death.
  • Storing durable user edits only in SavedStateHandle. Tapping Back or swiping the app away clears saved instance state permanently.

Follow-up questions

  • "How can you reproduce system process death locally without opening heavy games?" Press Home to background the app, then run adb shell am kill <package> (or use Android Studio's Terminate Application action) and reopen the app from Recents. That kills the process while keeping saved instance state intact.
  • "If one screen saves only 600 KB in its Bundle, why can it still throw TransactionTooLargeException?" The 1,024 KB Binder buffer is shared across all in-flight transactions in the process, including back-stack fragments and framework state. Two screens saving 600 KB and 450 KB total 1,050 KB and crash. Staying under 50 KB leaves safe headroom.
  • "When should you use Room or DataStore instead of SavedStateHandle?" Use SavedStateHandle for small, screen-scoped transient state such as a selected tab, scroll position, or item ID. Put durable user data, large lists, and binary files in Room, DataStore, or app files.

What a strong answer covers

Separates configuration change from process death and puts each value in the right tier. Cites the 1 MB Binder buffer and 50 KB target, and stores only a file URI in SavedStateHandle.

Open in interactive practice deck →

Original OfferVibe question. Android: Save UI states · Saved State module for ViewModel · TransactionTooLargeException.
© 2026 OfferVibe · Text CC BY-NC-SA 4.0 · code MITFree to share and adapt with credit, not for commercial use. Code samples are free for any use.