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

Composables

rememberSaveable

Remember the value produced by init.

Cmn
rememberSaveableStateHolder

Creates and remembers the instance of SaveableStateHolder.

Cmn
rememberSerializable

Remember the value produced by init, and persist it across activity or process recreation using a KSerializer for saving and restoring via the saved instance state mechanism.

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

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) },
    )
}

Top-level properties

LocalSaveableStateRegistry

val LocalSaveableStateRegistryProvidableCompositionLocal<SaveableStateRegistry?>

CompositionLocal with a current SaveableStateRegistry instance.