# SaveableStateRegistry

Artifact: [androidx.compose.runtime:runtime-saveable](https://developer.android.com/jetpack/androidx/releases/compose-runtime) [View Source](https://cs.android.com/search?q=file:androidx/compose/runtime/saveable/SaveableStateRegistry.kt+class:androidx.compose.runtime.saveable.SaveableStateRegistry) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.0.0) Common/All
- Cmn

  ```
  interface SaveableStateRegistry
  ```

*** ** * ** ***

Allows components to save and restore their state using the saved instance state mechanism.

## Summary

| ### Nested types |
|---|
| `interface https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/SaveableStateRegistry.Entry` The registry entry which you get when you use `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/SaveableStateRegistry#registerProvider(kotlin.String,kotlin.Function0)`. |

| ### Public functions |
|---|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/SaveableStateRegistry#canBeSaved(kotlin.Any)(value: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html)` Returns true if the value can be saved using this Registry. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/SaveableStateRegistry#consumeRestored(kotlin.String)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)` Returns the restored value for the given key. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-map/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?>>` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/SaveableStateRegistry#performSave()()` Executes all the registered value providers and combines these values into a map. | Cmn |
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/SaveableStateRegistry.Entry` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/SaveableStateRegistry#registerProvider(kotlin.String,kotlin.Function0)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html, valueProvider: () -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?)` Registers the value provider. | Cmn |

## Public functions

### canBeSaved

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.0.0)

```
fun canBeSaved(value: Any): Boolean
```

Returns true if the value can be saved using this Registry. The default implementation will return true if this value can be stored in Bundle.

| Parameters |
|---|---|
| `value: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html` | The value which we want to save using this Registry |

### consumeRestored

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.0.0)

```
fun consumeRestored(key: String): Any?
```

Returns the restored value for the given key. Once being restored the value is cleared, so you can't restore the same key twice.

| Parameters |
|---|---|
| `key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | Key used to save the value |

### performSave

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.0.0)

```
fun performSave(): Map<String, List<Any?>>
```

Executes all the registered value providers and combines these values into a map. We have a list of values for each key as it is allowed to have multiple providers for the same key.

### registerProvider

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.0.0)

```
fun registerProvider(key: String, valueProvider: () -> Any?): SaveableStateRegistry.Entry
```

Registers the value provider.

There are could be multiple providers registered for the same `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/SaveableStateRegistry#registerProvider(kotlin.String,kotlin.Function0)`. In this case the order in which they were registered matters.

Say we registered two providers for the key. One provides "1", second provides "2". `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/SaveableStateRegistry#performSave()` in this case will have listOf("1", "2) as a value for the key in the map. And later, when the registry will be recreated with the previously saved values, the first execution of `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/SaveableStateRegistry#consumeRestored(kotlin.String)` would consume "1" and the second one "2".

| Parameters |
|---|---|
| `key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | Key to use for storing the value |
| `valueProvider: () -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?` | Provides the current value, to be executed when `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/SaveableStateRegistry#performSave()` will be triggered to collect all the registered values |

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/SaveableStateRegistry.Entry` | the registry entry which you can use to unregister the provider |