skip to content

In Navigation 3, a tank-detail entry slides in through NavDisplay.TransitionKey metadata, yet a predictive back swipe still fades and scales it; why, and which transition wins?

level: seniorimportance: nice to knowfreq 22%

answer

  1. three directions, three specs
  2. metadata beats the NavDisplay default
  3. NavDisplay reads the Scene's metadata
  4. pop reads the outgoing scene
  5. predictive back has its own key

basics

~20 s

NavDisplay keeps three separate transitions: forward, pop and predictive pop. The entry set only TransitionKey, so a predictive back swipe falls back to NavDisplay's predictivePopTransitionSpec, whose Android default fades in and scales the old content to 0.7; set PredictivePopTransitionKey too.

solid answer

~40 s

`NavDisplay` takes `transitionSpec`, `popTransitionSpec` and `predictivePopTransitionSpec`, and a scene can override each through metadata keyed by `NavDisplay.TransitionKey`, `PopTransitionKey` and `PredictivePopTransitionKey`. Each direction is resolved on its own: metadata for that direction if present, otherwise the `NavDisplay` parameter. On Android the defaults are a 700 ms cross-fade for forward and pop, and for predictive back a spring fade-in with the exiting content scaling out to 0.7, which is the fade-and-scale seen here. NavDisplay reads only the `Scene`'s `metadata`, which by default is the metadata of the scene's last entry, so entry metadata works through the default scenes and a custom scene can override it. On a forward move the incoming scene supplies the spec; on a pop it is the outgoing one, so the detail entry carries its own pop and predictive-pop keys.

code

kotlin · 17 lines
kotlin
entry<TankDetail>(
    metadata = metadata {
        put(NavDisplay.TransitionKey) {
            slideInHorizontally(initialOffsetX = { it }) togetherWith
                slideOutHorizontally(targetOffsetX = { -it / 3 })
        }
        put(NavDisplay.PopTransitionKey) {
            slideInHorizontally(initialOffsetX = { -it / 3 }) togetherWith
                slideOutHorizontally(targetOffsetX = { it })
        }
        // Without this key, a back swipe uses NavDisplay's predictivePopTransitionSpec
        put(NavDisplay.PredictivePopTransitionKey) {
            slideInHorizontally(initialOffsetX = { -it / 3 }) togetherWith
                slideOutHorizontally(targetOffsetX = { it })
        }
    },
) { key -> TankDetailScreen(key.tankId) }

go deeper

for a junior

Recall that NavDisplay has a forward, a pop and a predictive pop transition, set as parameters for the whole app or as entry metadata for one screen.

for a middle

Explain the three metadata keys, the Android defaults, and that entry metadata reaches NavDisplay through the scene's metadata.

for a senior

Diagnose why one direction ignores a per-entry spec: a missing key for that direction, the outgoing scene supplying pop specs, a stack change NavDisplay treats as forward.

for a principal

Agree app-wide motion in NavDisplay's parameters with per-entry exceptions in metadata, and treat predictive back as a first-class direction in design review.

## Three directions, three specs `NavDisplay` animates between **scenes** with Compose's `AnimatedContent`, and every animation is a `ContentTransform` built by a lambda with an `AnimatedContentTransitionScope<Scene<T>>` receiver. It distinguishes three cases: | Case | NavDisplay parameter | Metadata key | Android default | |---|---|---|---| | Forward (keys added) | `transitionSpec` | `NavDisplay.TransitionKey` | `fadeIn` and `fadeOut` with `tween(700)` | | Pop (keys removed) | `popTransitionSpec` | `NavDisplay.PopTransitionKey` | the same 700 ms cross-fade | | Predictive back gesture | `predictivePopTransitionSpec` | `NavDisplay.PredictivePopTransitionKey` | spring `fadeIn` with `scaleOut(targetScale = 0.7f)` | The predictive variant's lambda also receives the swipe edge as an `Int`. The keys are `NavMetadataKey`s, set with the `metadata { put(...) }` DSL that arrived in Navigation 3 1.1.0. How `slideInHorizontally`, `togetherWith` and other `ContentTransform` parts work is animation material; here the question is which spec NavDisplay picks. ## Why the swipe fades and scales Each case is looked up separately. A detail entry that only sets `TransitionKey` customises the forward slide and nothing else. When the user swipes back, NavDisplay looks for `PredictivePopTransitionKey` in the outgoing scene's metadata, finds none, and uses its own `predictivePopTransitionSpec`, which by default is the fade-and-scale. A back that completes with no gesture in progress takes the pop path instead, and here that also falls back to NavDisplay's `popTransitionSpec`. The fix is to set all three keys on the entry, or to set app-wide defaults on `NavDisplay` itself. ## Which scene's metadata is read NavDisplay reads transitions from **`Scene.metadata` only**. The documented priority is the transitioning entry's metadata over the current scene's metadata over NavDisplay's defaults, and that works because `Scene.metadata` by default returns the metadata of the scene's last entry; a custom scene that overrides `metadata` decides for itself. The scene consulted is the one drawn on top during the change: 1. **Forward**: the incoming scene gets the higher z-index, so its metadata supplies `TransitionKey`. 2. **Pop**: the outgoing scene stays on top while it leaves, so *its* metadata supplies `PopTransitionKey`. The entry being popped controls how it leaves, not the entry being revealed. 3. **Predictive back**: the same outgoing scene supplies `PredictivePopTransitionKey`, while the gesture's progress seeks the transition. ## What counts as a pop NavDisplay compares the old and new lists of entries by their `contentKey`, not by the key objects themselves, and classifies the change: - the new list is a strict prefix of the old one: **pop**; - the first element changed, meaning the whole stack was replaced: **forward**; - the stack grew: **forward**; - the same size with a different top, for example `[Gallery, Detail(reef)]` to `[Gallery, Detail(koi)]`: **forward**, because the new list is not a prefix of the old one. A transition plays only when the scene changes: two scenes with the same class and `key` count as one, so a list-detail scene keyed by the gallery updates its detail pane without a scene animation. ## Reading the receiver Every spec lambda runs with an `AnimatedContentTransitionScope<Scene<*>>` receiver, so it can look at `initialState` and `targetState`, the scenes on either side of the change. That allows rules a fixed spec cannot express: - slide only when the target is the single-pane detail scene, and cross-fade when the layout switches to the two-pane scene; - skip motion entirely, with `EnterTransition.None togetherWith ExitTransition.None`, when the scene class does not change. `NavDisplay` also takes `sizeTransform`, applied to every scene change, and `contentAlignment` for the `AnimatedContent` it wraps. ## Practical rules - Put app-wide motion in `NavDisplay`'s three parameters; put exceptions in the entry's metadata. - When an entry overrides one direction, decide deliberately about the other two. - Treat predictive back separately: it is a gesture-driven seek, so pick a transform that reads well at partial progress. - For an element that should move between scenes, wrap `NavDisplay` in a `SharedTransitionLayout` and pass `sharedTransitionScope`, available since 1.1.0; without it, an entry that moves from one scene to another jumps. The shared element modifiers themselves belong to the animation material. - Test motion on a real back gesture as well as with the back button: the gesture path is the one most often left on the defaults. - Older code may use the map helpers `NavDisplay.transitionSpec { }` and friends; they write the same keys.

  • Why does the pop transition come from the tank-detail entry rather than the gallery it reveals?
    NavDisplay reads the transition from the scene drawn on top during the change. On a pop the outgoing scene keeps the higher z-index while it leaves, so its metadata, the detail entry's by default, supplies `PopTransitionKey`. The revealed gallery's pop key matters only when the gallery itself is popped.
  • The app replaces the stack [TankGallery, TankDetail] with a list that starts at a different key; which spec runs?
    The forward one. NavDisplay treats a change as a pop only when the new list is a strict prefix of the old; a changed first element counts as a replaced stack, so it takes `TransitionKey` from the incoming scene, or `transitionSpec` from NavDisplay.
  • How do you stop a tank photo from jumping when it moves between the single-pane and two-pane scenes?
    Wrap `NavDisplay` in a `SharedTransitionLayout` and pass its scope as `sharedTransitionScope`, available since Navigation 3 1.1.0. NavDisplay renders each entry in only one scene at a time, so without the scope the entry jumps. The shared element modifiers themselves are covered with the animation APIs.

  • PopTransitionKey also covers the predictive back gesture.
  • A pop takes its transition from the entry being revealed.
  • NavDisplay reads NavEntry metadata directly, so a custom Scene cannot change it.
  • NavDisplay's default transition is a horizontal slide.
  • Replacing the whole back stack plays the pop transition.

Assumes Navigation 3 1.2; the NavDisplay.TransitionKey, PopTransitionKey and PredictivePopTransitionKey metadata keys and sharedTransitionScope arrived in 1.1.0.