SceneStateKt

Added in 1.0.0-beta01

public final class SceneStateKt


Summary

Public methods

static final @NonNull SceneState<@NonNull T>
@Composable
<T extends Object> rememberSceneState(
    @NonNull List<@NonNull NavEntry<@NonNull T>> entries,
    @NonNull SceneStrategy<@NonNull T> sceneStrategy,
    @NonNull Function0<Unit> onBack
)

Returns a SceneState that is remembered across compositions based on the parameters.

Public methods

rememberSceneState

@Composable
public static final @NonNull SceneState<@NonNull T> <T extends Object> rememberSceneState(
    @NonNull List<@NonNull NavEntry<@NonNull T>> entries,
    @NonNull SceneStrategy<@NonNull T> sceneStrategy,
    @NonNull Function0<Unit> onBack
)

Returns a SceneState that is remembered across compositions based on the parameters.

This calculates all of the scenes and provides them in a SceneState.

import androidx.compose.material3.Text
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.navigation3.runtime.NavEntry
import androidx.navigation3.runtime.rememberDecoratedNavEntries
import androidx.navigation3.scene.SceneInfo
import androidx.navigation3.scene.SinglePaneSceneStrategy
import androidx.navigation3.scene.rememberSceneState
import androidx.navigation3.ui.NavDisplay
import androidx.navigationevent.compose.NavigationBackHandler
import androidx.navigationevent.compose.rememberNavigationEventState

val backStack = rememberSaveable { mutableStateListOf("a", "b") }
val entries =
    rememberDecoratedNavEntries(backStack) { key -> NavEntry(key) { Text("Key = $key") } }
val sceneState =
    rememberSceneState(
        entries,
        SinglePaneSceneStrategy(),
        onBack = { backStack.removeLastOrNull() },
    )
val currentScene = sceneState.currentScene
val navigationEventState =
    rememberNavigationEventState(
        currentInfo = SceneInfo(currentScene),
        backInfo = sceneState.previousScenes.map { SceneInfo(it) },
    )
NavigationBackHandler(
    navigationEventState,
    isBackEnabled = currentScene.previousEntries.isNotEmpty(),
    onBackCompleted = {
        // Remove entries from the back stack until we've removed all popped entries
        repeat(entries.size - currentScene.previousEntries.size) {
            backStack.removeLastOrNull()
        }
    },
)
NavDisplay(sceneState, navigationEventState)
Parameters
@NonNull List<@NonNull NavEntry<@NonNull T>> entries

all of the entries that are associated with this state

@NonNull SceneStrategy<@NonNull T> sceneStrategy

the SceneStrategy to determine which scene to render a list of entries.

@NonNull Function0<Unit> onBack

a callback for handling system back press.