Pick the fix
A form loses its typed text when the user switches the phone to dark mode. The team locked the screen to portrait to fix rotation. What's the right fix?
Hint 1 of 1
Is rotation the only thing that recreates an Activity?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A Keep the input in rememberSaveable or a ViewModel's SavedStateHandle
Correct. Dark mode is a configuration change too; state has to live where it survives recreation.
Why not the others?
- B Also lock dark mode in the manifest. There are many configuration changes; you can't lock them all.
- C Save the text in a static field. It survives recreation but not process death, and it leaks between screens.
- D Call
recreate()yourself. That's what's causing the loss.
Illustrative, based on Compose and Android rules; not a device recording
Text version
Rotate
- A
onPause - A
onStop - A
onDestroy - A'
onCreate - A'
onStart - A'
onResume
Dark mode on
- A
onPause - A
onStop - A
onDestroy - A'
onCreate - A'
onStart - A'
onResume
| State | Rotate | Dark mode on |
|---|---|---|
| local var | ✕ lost | ✕ lost |
| remember | ✕ lost | ✕ lost |
| remember | ✓ kept | ✓ kept |
| ViewModel | ✓ kept | ✓ kept |
| Saved | ✓ kept | ✓ kept |
| Room / disk | ✓ kept | ✓ kept |