# SaveableStateHolder

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/SaveableStateHolder.kt+class:androidx.compose.runtime.saveable.SaveableStateHolder) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.0.0) Common/All
- Cmn

  ```
  interface SaveableStateHolder
  ```

*** ** * ** ***

Allows to save the state defined with `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/rememberSaveable.composable#rememberSaveable(kotlin.Array,androidx.compose.runtime.saveable.Saver,kotlin.String,kotlin.Function0)` for the subtree before disposing it to make it possible to compose it back next time with the restored state. It allows different navigation patterns to keep the ui state like scroll position for the currently not composed screens from the backstack.

```kotlin
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.saveable.rememberSaveableStateHolder
import androidx.compose.ui.Modifier

@Composable
fun <T : Any> Navigation(
    currentScreen: T,
    modifier: Modifier = Modifier,
    content: @Composable (T) -> Unit,
) {
    // create SaveableStateHolder.
    val saveableStateHolder = rememberSaveableStateHolder()
    Box(modifier) {
        // Wrap the content representing the `currentScreen` inside `SaveableStateProvider`.
        // Here you can also add a screen switch animation like Crossfade where during the
        // animation multiple screens will be displayed at the same time.
        saveableStateHolder.SaveableStateProvider(currentScreen) { content(currentScreen) }
    }
}

Column {
    var screen by rememberSaveable { mutableStateOf("screen1") }
    Row(horizontalArrangement = Arrangement.SpaceEvenly) {
        Button(onClick = { screen = "screen1" }) { Text("Go to screen1") }
        Button(onClick = { screen = "screen2" }) { Text("Go to screen2") }
    }
    Navigation(screen, Modifier.fillMaxSize()) { currentScreen ->
        if (currentScreen == "screen1") {
            Screen1()
        } else {
            Screen2()
        }
    }
}
```

The content should be composed using `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/SaveableStateHolder#SaveableStateProvider(kotlin.Any,kotlin.Function0)` while providing a key representing this content. Next time `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/SaveableStateHolder#SaveableStateProvider(kotlin.Any,kotlin.Function0)` will be used with the same key its state will be restored.

## Summary

| ### Public functions |
|---|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composable https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/SaveableStateHolder#SaveableStateProvider(kotlin.Any,kotlin.Function0)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html, content: @https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composable () -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)` Put your content associated with a `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/SaveableStateHolder#SaveableStateProvider(kotlin.Any,kotlin.Function0)` inside the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/SaveableStateHolder#SaveableStateProvider(kotlin.Any,kotlin.Function0)`. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/SaveableStateHolder#removeState(kotlin.Any)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html)` Removes the saved state associated with the passed `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/SaveableStateHolder#removeState(kotlin.Any)`. | Cmn |

| ### Public properties |
|---|---|---|
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-set/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html>` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/SaveableStateHolder#keys()` Returns the set of all keys currently registered in this `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/SaveableStateHolder`. | Cmn |

## Public functions

### SaveableStateProvider

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

```
@Composable
fun SaveableStateProvider(key: Any, content: @Composable () -> Unit): Unit
```

Put your content associated with a `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/SaveableStateHolder#SaveableStateProvider(kotlin.Any,kotlin.Function0)` inside the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/SaveableStateHolder#SaveableStateProvider(kotlin.Any,kotlin.Function0)`. This will automatically save all the states defined with `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/rememberSaveable.composable#rememberSaveable(kotlin.Array,androidx.compose.runtime.saveable.Saver,kotlin.String,kotlin.Function0)` before disposing the content and will restore the states when you compose with this key again.

| Parameters |
|---|---|
| `key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html` | to be used for saving and restoring the states for the subtree. Note that on Android you can only use types which can be stored inside the Bundle. |
| `content: @https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composable () -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | the content for which `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/SaveableStateHolder#SaveableStateProvider(kotlin.Any,kotlin.Function0)` is associated. |

### removeState

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

```
fun removeState(key: Any): Unit
```

Removes the saved state associated with the passed `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/SaveableStateHolder#removeState(kotlin.Any)`.

## Public properties

### keys

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

```
open val keys: Set<Any>
```

Returns the set of all keys currently registered in this `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/SaveableStateHolder`.

These are the keys that were passed to `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/SaveableStateHolder#SaveableStateProvider(kotlin.Any,kotlin.Function0)` and have not yet been removed via `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/SaveableStateHolder#removeState(kotlin.Any)`.