Predict the output
The user types 'hello' into both fields, then rotates. What remains?
@Composable
fun Form() {
var a by remember { mutableStateOf("") }
var b by rememberSaveable { mutableStateOf("") }
Column {
TextField(value = a, onValueChange = { a = it })
TextField(value = b, onValueChange = { b = it })
}
}Hint 1 of 1
Is the Activity recreated on rotation?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A The first field is empty, the second shows hello
Correct. Rotation recreates the Activity; only rememberSaveable restores from the Bundle.
Why not the others?
- B Both show hello. remember is lost when the Activity is recreated.
- C Both are empty. rememberSaveable restores its value.
- D The app crashes because String isn't saveable. String fits in a Bundle.
Illustrative, based on Compose and Android rules; not a device recording
Text version
Rotate
- A
onPause - A
onStop - A
onSave Instance State - A
onDestroy - A'
onCreate - A'
onStart - A'
onResume
Process death
- A
onPause - A
onStop - A
onSave Instance State - (process killed: no more callbacks)
- A'
onCreate (with saved Bundle)
| State | Rotate | Process death |
|---|---|---|
| local var | ✕ lost | ✕ lost |
| remember | ✕ lost | ✕ lost |
| remember | ✓ kept | ✓ kept |
| ViewModel | ✓ kept | ✕ lost |
| Saved | ✓ kept | ✓ kept |
| Room / disk | ✓ kept | ✓ kept |