rememberSerializable

Composable Function

Common
@Composable
public inline fun <reified 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.

Parameters

inputsA 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.
configurationOptional SavedStateConfiguration to customize how the serialized data is stored and restored. Defaults to SavedStateConfiguration.DEFAULT.
initA factory function used to provide the initial value when no previously saved value exists.

Returns

The remembered and possibly restored value.
Common
@Composable
public 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.

Parameters

inputsA 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.
serializerA KSerializer used to serialize and deserialize the value.
configurationOptional SavedStateConfiguration to customize how the serialized data is stored and restored. Defaults to SavedStateConfiguration.DEFAULT.
initA factory function used to provide the initial value when no previously saved value exists.

Returns

The remembered and possibly restored value.
Common
@Composable
public inline fun <reified 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.

Parameters

inputsA 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.
configurationOptional SavedStateConfiguration to customize how the serialization is handled, such as specifying a custom format (e.g. JSON). Defaults to SavedStateConfiguration.DEFAULT.
initA factory function to produce the initial MutableState to be remembered.

Returns

The remembered and possibly restored MutableState.
Common
@Composable
public 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. This is particularly useful for custom or complex data types that are @Serializable.

Parameters

inputsA 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.
stateSerializerA KSerializer used to serialize and deserialize the state value. The value must be a non-nullable type marked with @Serializable.
configurationOptional SavedStateConfiguration to customize how the serialization is handled, such as specifying a custom format (e.g. JSON).
initA factory function to produce the initial value to be remembered and saved.