# SceneStrategyScope

Artifact: [androidx.navigation3:navigation3-ui](https://developer.android.com/jetpack/androidx/releases/navigation3) [View Source](https://cs.android.com/search?q=file:androidx/navigation3/scene/SceneStrategy.kt+class:androidx.navigation3.scene.SceneStrategyScope) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/navigation3#1.0.0) Common/All
- Cmn

  ```
  open class SceneStrategyScope<T : Any>
  ```
Known direct subclasses [SceneDecoratorStrategyScope](https://developer.android.com/reference/kotlin/androidx/navigation3/scene/SceneDecoratorStrategyScope)

|---|---|
| `https://developer.android.com/reference/kotlin/androidx/navigation3/scene/SceneDecoratorStrategyScope` | Scope used to create a `https://developer.android.com/reference/kotlin/androidx/navigation3/scene/Scene` from another `https://developer.android.com/reference/kotlin/androidx/navigation3/scene/Scene`. |

*** ** * ** ***

Scope used to create a `https://developer.android.com/reference/kotlin/androidx/navigation3/scene/Scene` from a list of `https://developer.android.com/reference/kotlin/androidx/navigation3/runtime/NavEntry`s.

This Scope should be provided to the `https://developer.android.com/reference/kotlin/androidx/navigation3/scene/SceneStrategy#(androidx.navigation3.scene.SceneStrategyScope).calculateScene(kotlin.collections.List)` function to create Scenes.

## Summary

| ### Public constructors |
|---|---|
| `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html> https://developer.android.com/reference/kotlin/androidx/navigation3/scene/SceneStrategyScope#SceneStrategyScope()()` Construct a `https://developer.android.com/reference/kotlin/androidx/navigation3/scene/SceneStrategyScope` suitable for calling `https://developer.android.com/reference/kotlin/androidx/navigation3/scene/SceneStrategy` functions in isolation. | Cmn |

| ### Public properties |
|---|---|---|
| `() -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/navigation3/scene/SceneStrategyScope#onBack()` A callback that should be connected to any internal handling of system back done by the returned `https://developer.android.com/reference/kotlin/androidx/navigation3/scene/Scene`. | Cmn |

## Public constructors

### SceneStrategyScope

Cmn

```
<T : Any> SceneStrategyScope()
```

Construct a `https://developer.android.com/reference/kotlin/androidx/navigation3/scene/SceneStrategyScope` suitable for calling `https://developer.android.com/reference/kotlin/androidx/navigation3/scene/SceneStrategy` functions in isolation.

For more complicated cases, such as ones where you want to test if `https://developer.android.com/reference/kotlin/androidx/navigation3/scene/SceneStrategyScope#onBack()` is called correctly, use `https://developer.android.com/reference/kotlin/androidx/navigation3/scene/rememberSceneState.composable#rememberSceneState(kotlin.collections.List,kotlin.collections.List,kotlin.collections.List,androidx.compose.animation.SharedTransitionScope,kotlin.Function0)`, which will construct its own internal `https://developer.android.com/reference/kotlin/androidx/navigation3/scene/SceneStrategyScope` suitable for a Scene that closely mirror real scenarios and be passed to `https://developer.android.com/reference/kotlin/androidx/navigation3/ui/NavDisplay`.

## Public properties

### onBack

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

```
val onBack: () -> Unit
```

A callback that should be connected to any internal handling of system back done by the returned `https://developer.android.com/reference/kotlin/androidx/navigation3/scene/Scene`.

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

```kotlin
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() }
    }

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (other !is SimpleDialogScene<*>) return false
        return entry == other.entry && previousEntries == other.previousEntries
    }

    override fun hashCode(): Int {
        return entry.hashCode() * 31 + previousEntries.hashCode()
    }
}

/**
 * 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
        }
    }
}
```