SavedStateSupport

A class helps to support saved state in Compose.

The clear method will clear all saved state and clear the ViewModelStore. Usually, you should call clear in onDispose of a androidx.compose.runtime.DisposableEffect that runs in the root @Composable.

The performSave method will save all the saved state. Usually, you should call performSave in onDispose of a androidx.compose.runtime.DisposableEffect that runs in a child @Composable. That androidx.compose.runtime.DisposableEffect should be at the last of the child @Composable, because SaveableStateRegistry.Entrys are unregistered in reverse order. We want to save state first, before SaveableStateRegistry.Entrys are unregistered.

Example

// Remember a SavedStateSupport instance.
val savedStateSupport = remember { SavedStateSupport() }

// Clear the SavedStateSupport when the root @Composable exits the composition.
savedStateSupport.ClearOnDispose()

// Provide SavedStateSupport as ViewModelStoreOwner, SaveableStateRegistry and SavedStateHandleFactory.
savedStateSupport.ProvideCompositionLocals {
MyApp()
}

Constructors

Link copied to clipboard
constructor()

Types

Link copied to clipboard

Properties

Link copied to clipboard
open override val viewModelStore: ViewModelStore

Functions

Link copied to clipboard
fun addCloseable(key: Any, strategy: SavedStateSupport.CloseableStrategy = CloseableStrategy.CloseIfChanged, closeable: Closeable)

Add a Closeable that will be closed when clear is called.

Link copied to clipboard
open override fun canBeSaved(value: Any): Boolean
Link copied to clipboard
fun clear()
Link copied to clipboard

Clear this SavedStateSupport when the @Composable exits the composition.

Link copied to clipboard
open override fun consumeRestored(key: String): Any?
Link copied to clipboard
open override fun create(): SavedStateHandle
Link copied to clipboard

Get a Closeable by key.

Link copied to clipboard
open override fun performSave(): Map<String, List<Any?>>
Link copied to clipboard
open override fun registerProvider(key: String, valueProvider: () -> Any?): SaveableStateRegistry.Entry
Link copied to clipboard
fun removeCloseable(key: Any, close: Boolean = true): Closeable?

Remove a Closeable by key and close it if close is true.