respawnMutableStateFlow

inline fun <T : @Serializable Any> SavedStateHandle.respawnMutableStateFlow(init: () -> T, key: String = RespawnDefaults.DEFAULT_KEY, configuration: SavedStateConfiguration = SavedStateConfiguration.DEFAULT): 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.