rememberSaveable vs remember in Jetpack Compose: Keeping State Across Configuration Changes
The difference between rememberSaveable and remember shows up on configuration changes: remember keeps a value only while the current composition and Activity instance live, so a screen rotation resets it. rememberSaveable also writes the value into the saved instance state, so it comes back after rotation and after the system kills and recreates your app process in the background.

What happens to remember state on screen rotation
By default, a configuration change such as rotating the device, switching to dark mode, changing the system language or resizing the window in multi-window mode destroys the Activity and creates a new one. setContent runs again, Compose builds a fresh composition, and every remember block runs its initializer from the start. A counter goes back to zero, a half-typed search query disappears and an expanded card collapses.
@Composable
fun Counter() {
var count by remember { mutableStateOf(0) }
Button(onClick = { count++ }) {
Text("Clicked $count times")
}
}
// Rotate the device: count is 0 againremember is still the right tool for surviving recomposition, which is what it was built for; the basics are in State Management in Android Compose. Declaring android:configChanges in the manifest stops the Activity from being recreated, but it is an opt-in workaround and does nothing for process death.
How rememberSaveable stores values and which types it supports
rememberSaveable registers the value with a SaveableStateRegistry, which is backed by the Activity's saved instance state Bundle. Compose generates a key from the composable's position in the composition, so when the same call site enters composition again after recreation, the value is restored instead of recalculated. Out of the box it accepts anything a Bundle can hold:
Primitive types such as
Int,Long,Float,DoubleandBoolean, plusStringParcelableobjects, including data classes annotated with@ParcelizeSerializableobjectsA
MutableStatethat wraps one of the types above, sorememberSaveable { mutableStateOf("") }works without extra code
@Composable
fun SearchBox() {
var query by rememberSaveable { mutableStateOf("") }
TextField(value = query, onValueChange = { query = it })
}
// TextFieldValue also keeps cursor and selection, and ships its own Saver
var field by rememberSaveable(stateSaver = TextFieldValue.Saver) {
mutableStateOf(TextFieldValue(""))
}The saved state travels through a Binder transaction that has a size limit. Keep it to small UI values like IDs, flags, text input and selections. Lists of loaded items, bitmaps or whole API responses do not belong here. When I review a screen, this is what I check first: is every rememberSaveable holding something small enough to describe in one line?
Custom Saver examples for non-parcelable objects
When the type is not something a Bundle understands, you tell Compose how to turn it into saveable pieces and back. There are a few ways to do that, from least to most code.
@Parcelize when you own the class
// requires the kotlin-parcelize Gradle plugin
@Parcelize
data class City(val name: String, val country: String) : Parcelable
var city by rememberSaveable { mutableStateOf(City("Istanbul", "Turkey")) }mapSaver for classes you cannot annotate
data class City(val name: String, val country: String)
val CitySaver = run {
val nameKey = "name"
val countryKey = "country"
mapSaver(
save = { mapOf(nameKey to it.name, countryKey to it.country) },
restore = { City(it[nameKey] as String, it[countryKey] as String) }
)
}
var city by rememberSaveable(stateSaver = CitySaver) {
mutableStateOf(City("Istanbul", "Turkey"))
}listSaver for a shorter version
val CitySaver = listSaver<City, Any>(
save = { listOf(it.name, it.country) },
restore = { City(it[0] as String, it[1] as String) }
)A Saver for your own state holder
For a plain state holder class, put a Saver in its companion object and pass it with the saver parameter. This is the same pattern Compose uses for LazyListState and LazyGridState, which is why scroll position in a LazyVerticalGrid survives rotation without any work on your side.
class FilterState(initialExpanded: Boolean, initialTag: String) {
var expanded by mutableStateOf(initialExpanded)
var tag by mutableStateOf(initialTag)
companion object {
val Saver = listSaver<FilterState, Any>(
save = { listOf(it.expanded, it.tag) },
restore = { FilterState(it[0] as Boolean, it[1] as String) }
)
}
}
@Composable
fun rememberFilterState() = rememberSaveable(saver = FilterState.Saver) {
FilterState(initialExpanded = false, initialTag = "all")
}When to move state to a ViewModel instead
rememberSaveable is meant for UI element state: what is expanded, what is selected, what the user typed, where a list is scrolled. Move the state to a ViewModel when:
It comes from a repository, the network or a database
Several composables or screens read and change it
It is tied to business logic, such as validation or submitting a form
It is too large to fit comfortably in a
Bundle
A ViewModel survives configuration changes but not process death on its own. For the small pieces that must come back after the process is killed, such as a selected ID or a filter, use SavedStateHandle inside the ViewModel and reload the rest from your data layer. With Navigation Compose each back stack entry gets its own saved state scope, which is covered in Advanced Navigation Techniques in Jetpack Compose.
Common mistakes
Saving a type the Bundle cannot hold. Compose throws an
IllegalArgumentExceptionsaying the value cannot be saved using the currentSaveableStateRegistry. Add@Parcelizeor a customSaver.Putting loaded data into rememberSaveable. Large lists push the saved state towards the transaction limit. Save an ID and load the data again.
Keeping the same value in two places. If a
ViewModelalready owns a field, mirroring it withrememberSaveablecreates two sources of truth that drift apart.Forgetting inputs.
rememberSaveable(userId) { ... }resets the value whenuserIdchanges, just likerememberwith keys. Without the input, a new user sees the previous user's state.Testing only rotation. Rotation does not show process death. The test I run before shipping a screen: enable "Don't keep activities" in developer options, or put the app in the background and run
adb shell am kill <your.package>, then return to it.
Short code comparison
@Composable
fun Comparison() {
// Survives recomposition only. Lost on rotation and process death.
var tapsRemember by remember { mutableStateOf(0) }
// Survives recomposition, rotation and process death.
var tapsSaveable by rememberSaveable { mutableStateOf(0) }
Column {
Button(onClick = { tapsRemember++ }) { Text("remember: $tapsRemember") }
Button(onClick = { tapsSaveable++ }) { Text("rememberSaveable: $tapsSaveable") }
}
}Tap both buttons a few times and rotate the device: the first counter resets, the second keeps its value.
Frequently asked questions
Does rememberSaveable survive the user closing the app?
No. If the user swipes the app away from recents or presses back to finish the Activity, the saved instance state is cleared. Use a database or DataStore for data that must outlive that.
Is rememberSaveable slower than remember?
Reading the value costs the same during recomposition. The extra work happens when the Activity saves its state, which is why only small values should go into it.
Can I use rememberSaveable inside a LazyColumn item?
Yes. Items that scroll off screen and come back keep their saved value, as long as each item has a stable key passed to items(key = ...).
