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

RestorableStateHolder

interface RestorableStateHolder<T : Any>
androidx.compose.runtime.savedinstancestate.RestorableStateHolder

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.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.savedinstancestate.rememberRestorableStateHolder
import androidx.compose.runtime.savedinstancestate.savedInstanceState

@Composable
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.RestorableStateProvider(currentScreen) {
            content(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") {
            Screen1()
        } else {
            Screen2()
        }
    }
}
The content should be composed using RestorableStateProvider while providing a key representingthis content. Next time RestorableStateProvider will be used with the same key its state will berestored.

Summary

Public methods
abstract Unit
RestorableStateProvider(key: T, content: () -> Unit)

Put your content associated with a key inside the content.

abstract Unit
removeState(key: T)

Removes the saved state associated with the passed key.

Public methods

RestorableStateProvider

@Composable abstract fun RestorableStateProvider(
    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.

Parameters
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.

removeState

abstract fun removeState(key: T): Unit

Removes the saved state associated with the passed key.