Google is committed to advancing racial equity for Black communities. See how.


interface RestorableStateHolder<T : Any>

Allows to save the state defined with savedInstanceState and rememberSavedInstanceState 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.

import androidx.compose.material.Text
import androidx.compose.runtime.savedinstancestate.rememberRestorableStateHolder
import androidx.compose.runtime.savedinstancestate.savedInstanceState

fun <T : Any> Navigation(
    currentScreen: T,
    modifier: Modifier = Modifier,
    content: @Composable (T) -> Unit
) {
    // create RestorableStateHolder.
    val restorableStateHolder = rememberRestorableStateHolder<T>()
    // wrap the content representing the `screen` key inside `withRestorableState`.
    // you can add screen switch animations where during the animation multiple screens
    // will displayed at the same time.
    Box(modifier) {
        restorableStateHolder.withRestorableState(currentScreen) {

Column {
    var screen by savedInstanceState { "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") {
        } else {
The content should be composed using withRestorableState while providing a key representingthis content. Next time withRestorableState will be used with the same key its state will berestored.


Public methods
abstract Unit
removeState(key: T)

Removes the saved state associated with the passed key.

abstract Unit
withRestorableState(key: T, content: () -> Unit)

Put your content associated with a key inside the content.

Public methods


abstract fun removeState(key: T): Unit

Removes the saved state associated with the passed key.


@Composable abstract fun withRestorableState(
    key: T,
    content: () -> Unit
): Unit

Put your content associated with a key inside the content. This will automatically save all the states defined with savedInstanceState and rememberSavedInstanceState before disposing the content and will restore the states when you compose with this key again.

key: T 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.