In Navigation 3, how do SceneStrategy and Scene show a tank gallery and tank detail side by side on tablets, and a feeding-times entry as a dialog?
answer
- one back stack, many layouts
- metadata marks which entries qualify
- first non-null Scene wins
- overlay strategy goes first
- previousEntries decides what back does
basics
~20 sNavDisplay tries each SceneStrategy in sceneStrategies in order and renders the first Scene returned. A custom strategy returns a two-pane Scene on wide windows when metadata marks gallery and detail; DialogSceneStrategy, listed first, shows dialog-marked entries in a Dialog.
solid answer
~40 sA `Scene` renders one or more `NavEntry`s: it has a `key`, its `entries`, the `previousEntries` that back would leave, a `content` composable and `metadata`. A `SceneStrategy` builds one in `SceneStrategyScope<T>.calculateScene(entries)` or returns `null` to pass. `NavDisplay(sceneStrategies = listOf(...))` tries the strategies in order and falls back to `SinglePaneSceneStrategy`. For the tablet layout, mark the gallery and detail entries with your own `NavMetadataKey`s, and have the strategy return a list-detail scene when the window is wide, the last entry is a detail and a gallery entry sits below it. For the feeding-times entry, add `DialogSceneStrategy.dialog()` metadata and put `DialogSceneStrategy()` first; it returns an overlay scene, and the entries beneath are run through the strategies again. Since 1.1 the parameter is a list; the single-`sceneStrategy` overloads are hidden-deprecated in 1.2.
code
kotlin · 26 linesobject GalleryPaneKey : NavMetadataKey<Boolean>
object DetailPaneKey : NavMetadataKey<Boolean>
data class TankListDetailScene<T : Any>(
override val key: Any,
override val previousEntries: List<NavEntry<T>>,
val gallery: NavEntry<T>,
val detail: NavEntry<T>,
) : Scene<T> {
override val entries: List<NavEntry<T>> = listOf(gallery, detail)
override val content: @Composable () -> Unit = {
Row(Modifier.fillMaxSize()) {
Box(Modifier.weight(0.4f)) { gallery.Content() }
Box(Modifier.weight(0.6f)) { detail.Content() }
}
}
}
class TankListDetailStrategy<T : Any>(private val isWide: Boolean) : SceneStrategy<T> {
override fun SceneStrategyScope<T>.calculateScene(entries: List<NavEntry<T>>): Scene<T>? {
if (!isWide) return null
val detail = entries.lastOrNull()?.takeIf { it.metadata.contains(DetailPaneKey) } ?: return null
val gallery = entries.findLast { it.metadata.contains(GalleryPaneKey) } ?: return null
return TankListDetailScene(gallery.contentKey, entries.dropLast(1), gallery, detail)
}
}go deeper
Recall that NavDisplay shows one entry by default and that a dialog entry is marked with DialogSceneStrategy.dialog() metadata plus DialogSceneStrategy in sceneStrategies.
Explain Scene and SceneStrategy: strategies tried in order, null means pass, a single-pane fallback, metadata as the signal for which entries qualify.
Build a list-detail strategy and defend its choices: scene key from the gallery, previousEntries for back, equals for scene identity, overlay strategies first.
Decide whether to own custom scenes or adopt Material's adaptive strategy, and keep layout rules in strategies and metadata so feature screens stay unaware of tablets.
## Scenes: the layout layer between the stack and the screen The back stack says *what* the user has navigated to; a **`Scene`** says *how* to lay it out. The interface has five members: - `key: Any`, which together with the scene's class identifies the scene and drives NavDisplay's top-level animation; - `entries`, the `NavEntry`s this scene renders, each at most once; - `previousEntries`, the entries that would remain after a back action; - `content`, the composable that places the entries; - `metadata`, which by default is the metadata of the last entry in `entries`. A **`SceneStrategy`** is a `fun interface` with one function, `SceneStrategyScope<T>.calculateScene(entries: List<NavEntry<T>>): Scene<T>?`. It receives all entries on the back stack and either claims some of them by returning a scene or returns `null`. The scope carries an `onBack` callback for scenes that handle back themselves, such as a separate window. ## How NavDisplay picks a scene 1. It passes the decorated entries to each strategy in `sceneStrategies`, in list order. 2. The first non-null scene wins; if none claims the entries, `SinglePaneSceneStrategy` shows the last entry alone. 3. If the winner is an **`OverlayScene`**, its `overlaidEntries` go through the strategies again from the first one, and this repeats until a non-overlay scene is found; that one renders underneath. 4. Scenes are compared with `equals`, so implementations should be data classes or override `equals` and `hashCode`; Navigation 3 1.2 adds a lint check for it. ## The tablet list-detail scene The strategy needs to know which entries qualify, so each entry says so through **metadata**: define `object GalleryPaneKey : NavMetadataKey<Boolean>` and `object DetailPaneKey : NavMetadataKey<Boolean>`, and set them with the `metadata { put(...) }` DSL in `entry<K>(metadata = ...)`. Then: - return `null` when the window is narrow, so phones fall through to the single pane; how the width is measured is a window-size question outside this subject; - require the **last** entry to carry `DetailPaneKey` and find the nearest gallery entry below it with `findLast`; - use the gallery's `contentKey` as the scene `key`, so opening another tank keeps the same scene identity and the detail pane swaps by recomposition instead of animating the whole scene; - set `previousEntries = entries.dropLast(1)`. `previousEntries` is not just for animation. NavDisplay enables back only while it is non-empty, uses it to compute the predictive-back target scene, and on a completed back calls `onBack` once for each entry the scene would drop, `entries.size - previousEntries.size` times. ## The dialog entry Mark the entry with `entry<FeedingTimes>(metadata = DialogSceneStrategy.dialog())`, which puts `DialogProperties` under `DialogSceneStrategy.DialogKey`. `DialogSceneStrategy` checks only the last entry; when it carries that key it returns an overlay scene that shows the entry in a `Dialog` whose dismissal calls `onBack`. The documentation says to list it **before** any non-overlay strategy, because the first non-null scene wins and a non-overlay strategy that claimed the dialog's entry would render it inline. Entries under an overlay stay composed with their lifecycle capped at `STARTED`. | Strategy | Claims when | Returns | |---|---|---| | `DialogSceneStrategy()` | last entry has `DialogKey` metadata | an overlay scene in a `Dialog` | | your list-detail strategy | window is wide, last entry is a detail, a gallery entry is below it | a two-pane scene | | `SinglePaneSceneStrategy` (fallback) | always | the last entry alone | ## Testing and extending scene logic - `calculateScene` is a plain function, so a strategy can be unit-tested without a device: build entries, construct `SceneStrategyScope()`, whose public constructor exists for calling strategies in isolation, and assert on the returned scene or `null`. - When a test must check that a scene's own back handling reaches `onBack`, the documentation points to `rememberSceneState`, which builds the scope the way `NavDisplay` does. - Chrome shared by every non-overlay scene, such as a frame around all panes, belongs in a `SceneDecoratorStrategy` passed as `sceneDecoratorStrategies`, added in 1.1.0, rather than copied into each scene. ## Release notes that matter Navigation 3 1.1.0 replaced the single `sceneStrategy` parameter with `sceneStrategies: List<SceneStrategy<T>>`; in 1.2 the single-strategy `NavDisplay` overloads are deprecated at level `HIDDEN`, and `SceneStrategy.then` chaining is deprecated with a warning. Material 3's adaptive library ships a ready-made list-detail strategy; building your own, as here, shows the mechanism the interviewer is after.
- Why key the list-detail scene by the gallery entry's contentKey instead of the detail's?NavDisplay animates between scenes when the scene's class or `key` changes. Keyed by the gallery, opening another tank yields a scene with the same class and key, so the detail pane updates by recomposition and the gallery pane stays put. Keyed by the detail, every new tank would count as a new scene and NavDisplay would animate the whole two-pane layout.
- What does previousEntries control in a custom Scene?It is what back leaves behind. NavDisplay enables back handling only while it is non-empty, runs it through the strategies to build the predictive-back target scene, and on a completed back calls `onBack` once per entry being dropped, `entries.size - previousEntries.size` times. A scene that drops two entries on back must say so here.
- Why does the documentation say DialogSceneStrategy goes before non-overlay strategies?Strategies run in order and the first non-null scene wins. A non-overlay strategy listed earlier that accepts the top entry, such as a catch-all, would render the feeding-times entry inline and `DialogSceneStrategy` would never see it. Listed first, it claims the dialog entry, and the entries beneath run through the strategies again from the start.
- NavDisplay in 1.2 takes one sceneStrategy, chained with then for more layouts.
- A SceneStrategy that cannot handle the entries should throw an exception.
- A custom Scene can be a plain class compared by identity.
- A dialog destination needs its own NavDisplay and back stack.
- previousEntries only affects the predictive back animation.
Assumes Navigation 3 1.2: sceneStrategies is a list since 1.1.0, and the single sceneStrategy overloads are hidden-deprecated in 1.2.