SaveableStateRegistry

Coordinates values that survive composition disposal and host recreation.

Android hosts connect this registry to androidx.savedstate. Custom hosts install their own registry with ProvideSaveableStateRegistry. Restored values use a claim/commit protocol so an aborted composition cannot consume state that a later attempt still needs.

Samples

val registry = createSaveableStateRegistry(
    restoredValues = mapOf("counter" to 3),
    canBeSaved = { value -> value == null || value is Int },
)
val restored = checkNotNull(registry.claimRestored("counter"))
check(restored.value == 3)
restored.commit()

var counter = 4
val entry = registry.registerProvider("counter") { counter }
counter = 5
check(registry.performSave()["counter"] == 5)
entry.unregister()

Types

Link copied to clipboard
fun interface Entry

Lifecycle handle for one registered save provider.

Functions

Link copied to clipboard
abstract fun canBeSaved(value: Any?): Boolean

Returns whether value can cross the current host's persistence boundary.

Link copied to clipboard

Reserves restored state for one composition attempt.

Link copied to clipboard

Claims and immediately commits a restored value for key.

Link copied to clipboard
abstract fun performSave(): Map<String, Any?>

Returns a snapshot of restored, retained, claimed, and actively provided values.

Link copied to clipboard
abstract fun registerProvider(key: String, valueProvider: () -> Any?): SaveableStateRegistry.Entry

Registers one value provider for key.