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
@Composable
fun Counter() {
// Survives rotation — remember alone would not
var count by rememberSaveable { mutableStateOf(0) }
Column {
Text("Count: $count")
Button(onClick = { count++ }) { Text("Increment") }
}
}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`.