rememberSaveable

Composable Function

Common

Deprecated 'rememberSaveable' with a custom 'key' is no longer supported. It bypasses positional scoping, leading to state bugs and inconsistent behavior (e.g., unintentional state sharing or loss, issues in nested LazyLayouts). Please remove the 'key' parameter to use positional scoping for consistent, locally-scoped state. See https://r.android.com/3610053 for details.

@Composable
public fun <T : Any> rememberSaveable(
    vararg inputs: Any?,
    saver: Saver<T, out 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).

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.

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

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.

Parameters

inputsA set of inputs such that, when any of them have changed, will cause the state to reset and init to be rerun. Note that state restoration DOES NOT validate against inputs provided before value was saved.
saverThe Saver object which defines how the state is saved and restored.
keyAn 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
initA factory function to create the initial value of this state
Common
@Composable
public 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).

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.

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

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.

Parameters

inputsA set of inputs such that, when any of them have changed, will cause the state to reset and init to be rerun. Note that state restoration DOES NOT validate against inputs provided before value was saved.
initA factory function to create the initial value of this state
Common
@Composable
public fun <T : Any> rememberSaveable(
    vararg inputs: Any?,
    saver: Saver<T, out 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).

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.

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

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.

Parameters

inputsA set of inputs such that, when any of them have changed, will cause the state to reset and init to be rerun. Note that state restoration DOES NOT validate against inputs provided before value was saved.
saverThe Saver object which defines how the state is saved and restored.
initA factory function to create the initial value of this state
Common
@Composable
public fun <T> rememberSaveable(
    vararg inputs: Any?,
    stateSaver: Saver<T, out 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.

Parameters

inputsA set of inputs such that, when any of them have changed, will cause the state to reset and init to be rerun. Note that state restoration DOES NOT validate against inputs provided before value was saved.
stateSaverThe Saver object which defines how the value inside the MutableState is saved and restored.
initA factory function to create the initial value of this state
Common

Deprecated 'rememberSaveable' with a custom 'key' is no longer supported. It bypasses positional scoping, leading to state bugs and inconsistent behavior (e.g., unintentional state sharing or loss, issues in nested LazyLayouts). Please remove the 'key' parameter to use positional scoping for consistent, locally-scoped state. See https://r.android.com/3610053 for details.

@Composable
public fun <T> rememberSaveable(
    vararg inputs: Any?,
    stateSaver: Saver<T, out 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.

Parameters

inputsA set of inputs such that, when any of them have changed, will cause the state to reset and init to be rerun. Note that state restoration DOES NOT validate against inputs provided before value was saved.
stateSaverThe Saver object which defines how the value inside the MutableState is saved and restored.
keyAn 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
initA factory function to create the initial value of this state