rememberSerializable

Functions summary

inline T
@Composable
<T : Any> rememberSerializable(
    vararg inputs: Any?,
    configuration: SavedStateConfiguration,
    noinline init: () -> 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.

Cmn
inline MutableState<T>
@Composable
<T : Any> rememberSerializable(
    vararg inputs: Any?,
    configuration: SavedStateConfiguration,
    noinline init: () -> MutableState<T>
)

Remember a MutableState produced by init, and persist it across activity or process recreation using a KSerializer from kotlinx.serialization.

Cmn
T
@Composable
<T : Any> rememberSerializable(
    vararg inputs: Any?,
    serializer: KSerializer<T>,
    configuration: SavedStateConfiguration,
    init: () -> 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.

Cmn
MutableState<T>
@Composable
<T : Any> rememberSerializable(
    vararg inputs: Any?,
    stateSerializer: KSerializer<T>,
    configuration: SavedStateConfiguration,
    init: () -> MutableState<T>
)

Remember the value produced by init, and save it across activity or process recreation using a KSerializer from kotlinx.serialization.

Cmn

Functions

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 init to be re-executed. Note that previously saved values are not validated against these inputs during restoration.

configuration: SavedStateConfiguration = DEFAULT

Optional SavedStateConfiguration to customize how the serialized data is stored and restored. Defaults to SavedStateConfiguration.DEFAULT.

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.

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 init to be re-executed. Note that previously saved values are not validated against these inputs during restoration.

configuration: SavedStateConfiguration = DEFAULT

Optional SavedStateConfiguration to customize how the serialization is handled, such as specifying a custom format (e.g. JSON). Defaults to SavedStateConfiguration.DEFAULT.

noinline init: () -> MutableState<T>

A factory function to produce the initial MutableState to be remembered.

Returns
MutableState<T>

The remembered and possibly restored MutableState.

rememberSerializable

@Composable
fun <T : Any> rememberSerializable(
    vararg inputs: Any?,
    serializer: KSerializer<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 init to be re-executed. Note that previously saved values are not validated against these inputs during restoration.

serializer: KSerializer<T>

A KSerializer used to serialize and deserialize the value.

configuration: SavedStateConfiguration = DEFAULT

Optional SavedStateConfiguration to customize how the serialized data is stored and restored. Defaults to SavedStateConfiguration.DEFAULT.

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: KSerializer<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.KSerializer. 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 init will be rerun. Note: state restoration does NOT validate against inputs used before the value was saved.

stateSerializer: KSerializer<T>

A KSerializer used to serialize and deserialize the state value. The value must be a non-nullable type marked with @Serializable.

configuration: SavedStateConfiguration = DEFAULT

Optional SavedStateConfiguration to customize how the serialization is handled, such as specifying a custom format (e.g. JSON).

init: () -> MutableState<T>

A factory function to produce the initial value to be remembered and saved.