respawnMutableStateFlow

inline fun <T> SavedStateHandle.respawnMutableStateFlow(serializer: KSerializer<T>, init: () -> T, key: String = RespawnDefaults.DEFAULT_KEY, configuration: SavedStateConfiguration = SavedStateConfiguration.DEFAULT, crossinline onSerializationError: (Throwable) -> Unit = {}): MutableStateFlow<T>

Creates a MutableStateFlow that is automatically saved and restored across process death, using an explicit KSerializer.

This function bridges SavedStateHandle with kotlinx.serialization. When the process is killed, the current value of the flow is synchronously encoded into a SavedState bundle and preserved. When the process is recreated, the flow is initialized with the decoded state.

Important: This function also sets an internal flag indicating whether the state was successfully restored. You can query this flag using isRestored, onInit, or onRestore, but those methods must be called after this flow is instantiated.

Return

A MutableStateFlow containing the initial or restored state.

Parameters

serializer

The serializer to use for encoding and decoding the state.

init

A lambda that provides the initial value if no saved state is found, or if decoding fails.

key

The key used to store the state in the SavedStateHandle. Defaults to RespawnDefaults.DEFAULT_KEY.

configuration

The configuration to use when encoding/decoding the state.


inline fun <T> SavedStateHandle.respawnMutableStateFlow(init: () -> T, key: String = RespawnDefaults.DEFAULT_KEY, configuration: SavedStateConfiguration = SavedStateConfiguration.DEFAULT, crossinline onSerializationError: (Throwable) -> Unit = {}): MutableStateFlow<T>

Creates a MutableStateFlow that is automatically saved and restored across process death.

This function bridges SavedStateHandle with kotlinx.serialization. When the process is killed, the current value of the flow is synchronously encoded into a SavedState bundle and preserved. When the process is recreated, the flow is initialized with the decoded state.

Important: This function also sets an internal flag indicating whether the state was successfully restored. You can query this flag using isRestored, onInit, or onRestore, but those methods must be called after this flow is instantiated.

Return

A MutableStateFlow containing the initial or restored state.

Parameters

init

A lambda that provides the initial value if no saved state is found, or if decoding fails.

key

The key used to store the state in the SavedStateHandle. Defaults to RespawnDefaults.DEFAULT_KEY.

configuration

The configuration to use when encoding/decoding the state.