SceneStrategyScope



Scope used to create a Scene from a list of NavEntrys.

This Scope should be provided to the SceneStrategy.calculateScene function to create Scenes.

Summary

Public constructors

Construct a SceneStrategyScope suitable for calling SceneStrategy.calculateScene in isolation.

Cmn

Public properties

() -> Unit

A callback that should be connected to any internal handling of system back done by the returned Scene.

Cmn

Public constructors

SceneStrategyScope

<T : Any> SceneStrategyScope()

Construct a SceneStrategyScope suitable for calling SceneStrategy.calculateScene in isolation.

For more complicated cases, such as ones where you want to test if onBack is called correctly, use rememberSceneState, which will construct its own internal SceneStrategyScope suitable for a Scene that closely mirror real scenarios and be passed to androidx.navigation3.ui.NavDisplay.

Public properties

onBack

val onBack: () -> Unit

A callback that should be connected to any internal handling of system back done by the returned Scene.

For example, if your Scene uses a separate window that handles system back itself or if the UI present in your Scene allows users to go back via a custom gesture or affordance, this callback allows you to bubble up that event to the SceneState / androidx.navigation3.ui.NavDisplay that interfaces with the developer owned back stack.

import androidx.compose.runtime.Composable
import androidx.compose.ui.window.Dialog
import androidx.navigation3.runtime.NavEntry
import androidx.navigation3.scene.OverlayScene
import androidx.navigation3.scene.Scene
import androidx.navigation3.scene.SceneStrategy
import androidx.navigation3.scene.SceneStrategyScope

/** Class that shows a [NavEntry] as a [Dialog]. */
class SimpleDialogScene<T : Any>(
    private val entry: NavEntry<T>,
    override val previousEntries: List<NavEntry<T>>,
    onBack: () -> Unit,
) : OverlayScene<T> {
    override val key = entry.contentKey
    override val entries = listOf(entry)
    override val overlaidEntries = previousEntries

    override val content: @Composable () -> Unit = {
        Dialog(onDismissRequest = onBack) { entry.Content() }
    }
}

/**
 * A [SceneStrategy] that shows every screen above the first one as a dialog.
 *
 * In a real example, you'd have developers opt into displaying their entry as a dialog by
 * providing a companion method that provides [NavEntry.metadata].
 *
 * @see androidx.navigation3.scene.DialogSceneStrategy
 */
class SimpleDialogSceneStrategy<T : Any> : SceneStrategy<T> {
    override fun SceneStrategyScope<T>.calculateScene(entries: List<NavEntry<T>>): Scene<T>? {
        val entry = entries.lastOrNull() ?: return null
        val previousEntries = entries.dropLast(1)
        // Only show this as a dialog if there is something 'underneath' the dialog
        return if (previousEntries.isNotEmpty()) {
            SimpleDialogScene(entry, previousEntries, onBack)
        } else {
            null
        }
    }
}