androidx.compose.runtime.saveable

Interfaces

SaveableStateHolder

Allows to save the state defined with rememberSaveable for the subtree before disposing it to make it possible to compose it back next time with the restored state.

Cmn
SaveableStateRegistry

Allows components to save and restore their state using the saved instance state mechanism.

Cmn
SaveableStateRegistry.Entry

The registry entry which you get when you use registerProvider.

Cmn
Saver

The Saver describes how the object of Original class can be simplified and converted into something which is Saveable.

Cmn
SaverScope

Scope used in Saver.save.

Cmn

Top-level functions summary

SaveableStateRegistry
SaveableStateRegistry(
    restoredValues: Map<StringList<Any?>>?,
    canBeSaved: (Any) -> Boolean
)

Creates SaveableStateRegistry.

Cmn
Saver<Original, Saveable>
<Original : Any?, Saveable : Any> Saver(
    save: SaverScope.(value) -> Saveable?,
    restore: (value) -> Original?
)

The Saver describes how the object of Original class can be simplified and converted into something which is Saveable.

Cmn
Saver<T, Any>
<T : Any?> autoSaver()

The default implementation of Saver which does not perform any conversion.

Cmn
Saver<Original, Any>
<Original : Any?, Saveable : Any?> listSaver(
    save: SaverScope.(value) -> List<Saveable>,
    restore: (list: List<Saveable>) -> Original?
)

The Saver implementation which allows to represent your Original class as a list of Saveable values.

Cmn
Saver<T, Any>
<T : Any?> mapSaver(
    save: SaverScope.(value) -> Map<StringAny?>,
    restore: (Map<StringAny?>) -> T?
)

The Saver implementation which allows to represent your class as a map of values which can be saved individually.

Cmn
T
@Composable
<T : Any> rememberSaveable(
    vararg inputs: Any?,
    saver: Saver<T, Any>,
    key: String?,
    init: () -> T
)

Remember the value produced by init.

Cmn
MutableState<T>
@Composable
<T : Any?> rememberSaveable(
    vararg inputs: Any?,
    stateSaver: Saver<T, Any>,
    key: String?,
    init: () -> MutableState<T>
)

Remember the value produced by init.

Cmn
SaveableStateHolder

Creates and remembers the instance of SaveableStateHolder.

Cmn

Top-level properties summary

Top-level functions

SaveableStateRegistry

fun SaveableStateRegistry(
    restoredValues: Map<StringList<Any?>>?,
    canBeSaved: (Any) -> Boolean
): SaveableStateRegistry

Creates SaveableStateRegistry.

Parameters
restoredValues: Map<StringList<Any?>>?

The map of the restored values

canBeSaved: (Any) -> Boolean

Function which returns true if the given value can be saved by the registry

fun <Original : Any?, Saveable : Any> Saver(
    save: SaverScope.(value) -> Saveable?,
    restore: (value) -> Original?
): Saver<Original, Saveable>

The Saver describes how the object of Original class can be simplified and converted into something which is Saveable.

What types can be saved is defined by SaveableStateRegistry, by default everything which can be stored in the Bundle class can be saved. The implementations can check that the provided value can be saved via SaverScope.canBeSaved

You can pass the implementations of this class as a parameter for rememberSaveable.

import androidx.compose.runtime.saveable.Saver

data class Holder(var value: Int)

// this Saver implementation converts Holder object which we don't know how to save
// to Int which we can save
val HolderSaver = Saver<Holder, Int>(save = { it.value }, restore = { Holder(it) })
Parameters
save: SaverScope.(value) -> Saveable?

Defines how to convert the value into a saveable one. If null is returned the value will not be saved.

restore: (value) -> Original?

Defines how to convert the restored value back to the original Class. If null is returned the value will not be restored and would be initialized again instead.

fun <T : Any?> autoSaver(): Saver<T, Any>

The default implementation of Saver which does not perform any conversion.

It is used by rememberSaveable by default.

See also
Saver
fun <Original : Any?, Saveable : Any?> listSaver(
    save: SaverScope.(value) -> List<Saveable>,
    restore: (list: List<Saveable>) -> Original?
): Saver<Original, Any>

The Saver implementation which allows to represent your Original class as a list of Saveable values.

What types can be saved is defined by SaveableStateRegistry, by default everything which can be stored in the Bundle class can be saved.

You can use it as a parameter for rememberSaveable.

import androidx.compose.runtime.saveable.Saver
import androidx.compose.runtime.saveable.listSaver

data class Size(val x: Int, val y: Int)

val sizeSaver =
    listSaver<Size, Int>(save = { listOf(it.x, it.y) }, restore = { Size(it[0], it[1]) })
fun <T : Any?> mapSaver(
    save: SaverScope.(value) -> Map<StringAny?>,
    restore: (Map<StringAny?>) -> T?
): Saver<T, Any>

The Saver implementation which allows to represent your class as a map of values which can be saved individually.

What types can be saved is defined by SaveableStateRegistry, by default everything which can be stored in the Bundle class can be saved.

You can use it as a parameter for rememberSaveable.

import androidx.compose.runtime.saveable.Saver
import androidx.compose.runtime.saveable.mapSaver

data class User(val name: String, val age: Int)

val userSaver = run {
    val nameKey = "Name"
    val ageKey = "Age"
    mapSaver(
        save = { mapOf(nameKey to it.name, ageKey to it.age) },
        restore = { User(it[nameKey] as String, it[ageKey] as Int) }
    )
}
@Composable
fun <T : Any> rememberSaveable(
    vararg inputs: Any?,
    saver: Saver<T, Any> = autoSaver(),
    key: String? = null,
    init: () -> T
): T

Remember the value produced by init.

It behaves similarly to remember, but the stored value will survive the activity or process recreation using the saved instance state mechanism (for example it happens when the screen is rotated in the Android application).

import androidx.compose.runtime.saveable.rememberSaveable

val list = rememberSaveable { mutableListOf<Int>() }

If you use it with types which can be stored inside the Bundle then it will be saved and restored automatically using autoSaver, otherwise you will need to provide a custom Saver implementation via the saver param.

import androidx.compose.runtime.saveable.Saver
import androidx.compose.runtime.saveable.rememberSaveable

val holder = rememberSaveable(saver = HolderSaver) { Holder(0) }

You can use it with a value stored inside androidx.compose.runtime.mutableStateOf.

import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable

var value by rememberSaveable { mutableStateOf(({ "value" })()) }

If the value inside the MutableState can be stored inside the Bundle it would be saved and restored automatically, otherwise you will need to provide a custom Saver implementation via an overload with which has stateSaver param.

import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.Saver
import androidx.compose.runtime.saveable.rememberSaveable

val holder = rememberSaveable(stateSaver = HolderSaver) { mutableStateOf(Holder(0)) }
Parameters
vararg inputs: Any?

A set of inputs such that, when any of them have changed, will cause the state to reset and init to be rerun

saver: Saver<T, Any> = autoSaver()

The Saver object which defines how the state is saved and restored.

key: String? = null

An optional key to be used as a key for the saved value. If not provided we use the automatically generated by the Compose runtime which is unique for the every exact code location in the composition tree

init: () -> T

A factory function to create the initial value of this state

@Composable
fun <T : Any?> rememberSaveable(
    vararg inputs: Any?,
    stateSaver: Saver<T, Any>,
    key: String? = null,
    init: () -> MutableState<T>
): MutableState<T>

Remember the value produced by init.

It behaves similarly to remember, but the stored value will survive the activity or process recreation using the saved instance state mechanism (for example it happens when the screen is rotated in the Android application).

Use this overload if you remember a mutable state with a type which can't be stored in the Bundle so you have to provide a custom saver object.

import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.Saver
import androidx.compose.runtime.saveable.rememberSaveable

val holder = rememberSaveable(stateSaver = HolderSaver) { mutableStateOf(Holder(0)) }
Parameters
vararg inputs: Any?

A set of inputs such that, when any of them have changed, will cause the state to reset and init to be rerun

stateSaver: Saver<T, Any>

The Saver object which defines how the value inside the MutableState is saved and restored.

key: String? = null

An optional key to be used as a key for the saved value. If not provided we use the automatically generated by the Compose runtime which is unique for the every exact code location in the composition tree

init: () -> MutableState<T>

A factory function to create the initial value of this state

rememberSaveableStateHolder

@Composable
fun rememberSaveableStateHolder(): SaveableStateHolder

Creates and remembers the instance of SaveableStateHolder.

Top-level properties

LocalSaveableStateRegistry

val LocalSaveableStateRegistryProvidableCompositionLocal<SaveableStateRegistry?>

CompositionLocal with a current SaveableStateRegistry instance.