androidx.compose.runtime.saveable
Interfaces
SaveableStateHolder |
Allows to save the state defined with |
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 |
Cmn
|
Saver |
The |
Cmn
|
SaverScope |
Scope used in |
Cmn
|
Top-level functions summary
SaveableStateRegistry |
Creates |
Cmn
|
Saver<Original, Saveable> |
<Original : Any?, Saveable : Any> Saver(The |
Cmn
|
Saver<T, Any> |
The default implementation of |
Cmn
|
Saver<Original, Any> |
<Original : Any?, Saveable : Any?> listSaver(The |
Cmn
|
Saver<T, Any> |
<T : Any?> mapSaver(The |
Cmn
|
T |
@ComposableRemember the value produced by |
Cmn
|
T |
@ComposableRemember the value produced by |
Cmn
|
MutableState<T> |
@ComposableRemember the value produced by |
Cmn
|
T |
@ComposableThis function is deprecated. 'rememberSaveable' with a custom 'key' is no longer supported. |
Cmn
|
MutableState<T> |
@ComposableThis function is deprecated. 'rememberSaveable' with a custom 'key' is no longer supported. |
Cmn
|
SaveableStateHolder |
Creates and remembers the instance of |
Cmn
|
inline T |
@ComposableRemember the value produced by |
Cmn
|
inline MutableState<T> |
@ComposableRemember a |
Cmn
|
T |
@ComposableRemember the value produced by |
Cmn
|
MutableState<T> |
@ComposableRemember the value produced by |
Cmn
|
Top-level properties summary
ProvidableCompositionLocal<SaveableStateRegistry?> |
CompositionLocal with a current |
Cmn
|
Top-level functions
SaveableStateRegistry
fun SaveableStateRegistry(
restoredValues: Map<String, List<Any?>>?,
canBeSaved: (Any) -> Boolean
): SaveableStateRegistry
Creates SaveableStateRegistry.
Saver
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. |
autoSaver
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 |
listSaver
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]) })
mapSaver
fun <T : Any?> mapSaver(
save: SaverScope.(value) -> Map<String, Any?>,
restore: (Map<String, Any?>) -> 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) }, ) }
rememberSaveable
@Composable
fun <T : Any> rememberSaveable(vararg inputs: Any?, 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: () -> T |
A factory function to create the initial value of this state |
rememberSaveable
@Composable
fun <T : Any> rememberSaveable(
vararg inputs: Any?,
saver: Saver<T, Any>,
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 |
saver: Saver<T, Any> |
The |
init: () -> T |
A factory function to create the initial value of this state |
rememberSaveable
@Composable
fun <T : Any?> rememberSaveable(
vararg inputs: Any?,
stateSaver: Saver<T, Any>,
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 |
stateSaver: Saver<T, Any> |
The |
init: () -> MutableState<T> |
A factory function to create the initial value of this state |
rememberSaveable
@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 |
saver: Saver<T, Any> = autoSaver() |
The |
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 |
rememberSaveable
@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 |
stateSaver: Saver<T, Any> |
The |
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.
rememberSerializable
@Composable
inline fun <T : Any> rememberSerializable(
vararg inputs: Any?,
configuration: SavedStateConfiguration = DEFAULT,
noinline init: () -> T
): T
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.
This function automatically finds a KSerializer for the reified type T, making it a convenient way to use rememberSaveable with types that are Serializable.
This behaves similarly to remember, but will survive configuration changes (such as screen rotations) and process death by saving the value into the instance state.
import androidx.compose.runtime.saveable.rememberSerializable val holder = rememberSerializable(serializer = HolderSerializer) { Holder(0) }
| Parameters | |
|---|---|
vararg inputs: Any? |
A set of inputs which, when changed, will cause the stored state to reset and |
configuration: SavedStateConfiguration = DEFAULT |
Optional |
noinline init: () -> T |
A factory function used to provide the initial value when no previously saved value exists. |
| Returns | |
|---|---|
T |
The remembered and possibly restored value. |
| See also | |
|---|---|
rememberSerializable |
rememberSerializable
@Composable
inline fun <T : Any> rememberSerializable(
vararg inputs: Any?,
configuration: SavedStateConfiguration = DEFAULT,
noinline init: () -> MutableState<T>
): MutableState<T>
Remember a MutableState produced by init, and persist it across activity or process recreation using a KSerializer from kotlinx.serialization.
This function automatically finds a KSerializer for the reified type T, making it a convenient way to use rememberSaveable with types that are Serializable.
This behaves similarly to remember, but the state will survive configuration changes (like screen rotations) and process recreation. It is designed for state types that cannot be stored in a Bundle directly but can be serialized.
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.saveable.rememberSerializable val holder = rememberSerializable(stateSerializer = HolderSerializer) { mutableStateOf(Holder(0)) }
| Parameters | |
|---|---|
vararg inputs: Any? |
A set of inputs which, when changed, will cause the stored state to reset and |
configuration: SavedStateConfiguration = DEFAULT |
Optional |
noinline init: () -> MutableState<T> |
A factory function to produce the initial |
| Returns | |
|---|---|
MutableState<T> |
The remembered and possibly restored |
| See also | |
|---|---|
rememberSerializable |
rememberSerializable
@Composable
fun <T : Any> rememberSerializable(
vararg inputs: Any?,
serializer: <Error class: unknown class><T>,
configuration: SavedStateConfiguration = DEFAULT,
init: () -> T
): T
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.
This behaves similarly to remember, but will survive configuration changes (such as screen rotations) and process death by saving the value into the instance state using a serializer from Kotlinx Serialization.
The value will be serialized using the provided serializer, and the way it's saved can be customized using configuration.
If the type cannot be automatically handled by a default Saver, this overload provides a simpler and type-safe way to persist complex or custom types using Kotlinx Serialization.
import androidx.compose.runtime.saveable.rememberSerializable val holder = rememberSerializable(serializer = HolderSerializer) { Holder(0) }
| Parameters | |
|---|---|
vararg inputs: Any? |
A set of inputs which, when changed, will cause the stored state to reset and |
serializer: <Error class: unknown class><T> |
A KSerializer used to serialize and deserialize the value. |
configuration: SavedStateConfiguration = DEFAULT |
Optional |
init: () -> T |
A factory function used to provide the initial value when no previously saved value exists. |
| Returns | |
|---|---|
T |
The remembered and possibly restored value. |
rememberSerializable
@Composable
fun <T : Any> rememberSerializable(
vararg inputs: Any?,
stateSerializer: <Error class: unknown class><T>,
configuration: SavedStateConfiguration = DEFAULT,
init: () -> MutableState<T>
): MutableState<T>
Remember the value produced by init, and save it across activity or process recreation using a KSerializer from kotlinx.serialization.
This behaves similarly to remember, but the value will survive configuration changes (like screen rotations) and process recreation by saving it in the instance state using a serialization-based mechanism.
This overload is intended for cases where the state type cannot be stored directly in a Bundle, but can be serialized with kotlinx.serialization. This is particularly useful for custom or complex data types that are @Serializable.
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.saveable.rememberSerializable val holder = rememberSerializable(stateSerializer = HolderSerializer) { mutableStateOf(Holder(0)) }
| Parameters | |
|---|---|
vararg inputs: Any? |
A set of inputs such that, when any of them have changed, the state will reset and |
stateSerializer: <Error class: unknown class><T> |
A KSerializer used to serialize and deserialize the state value. The value must be a non-nullable type marked with |
configuration: SavedStateConfiguration = DEFAULT |
Optional |
init: () -> MutableState<T> |
A factory function to produce the initial value to be remembered and saved. |
Top-level properties
LocalSaveableStateRegistry
val LocalSaveableStateRegistry: ProvidableCompositionLocal<SaveableStateRegistry?>
CompositionLocal with a current SaveableStateRegistry instance.