ShellNavDisplay

fun <T : Any> ShellNavDisplay(entries: List<NavEntry<T>>, sceneStrategies: List<SceneStrategy<T>>, onBack: () -> Unit, modifier: Modifier = Modifier, transitionSpec: AnimatedContentTransitionScope<Scene<T>>.() -> ContentTransform, popTransitionSpec: AnimatedContentTransitionScope<Scene<T>>.() -> ContentTransform, activityBack: () -> Boolean = { true }, backEnabled: Boolean = true, predictivePopTransitionSpec: AnimatedContentTransitionScope<Scene<T>>.(swipeEdge: Int) -> ContentTransform = { popTransitionSpec() })(source)

A NavDisplay whose predictive back is Android's cross-activity back animation.

NavDisplay alone seeks one ContentTransform with the gesture's progress and plays whatever is left of it once back is invoked. Android's animation is not one timeline: while the finger is down the pages move toward one set of rectangles, and once back is invoked they move from wherever they are toward another, with a spring carrying the gesture's speed. No single transition can seek both.

So the display is only asked to show the two scenes. Its back handler listens on a private dispatcher, and a handler on the system dispatcher forwards the gesture to it at progress zero, which makes it compose the scene underneath and hold it, with a transition that changes nothing. Every scene is wrapped in a layer that CrossActivityBackMotion places in its rectangle each frame. When back is invoked the display completes the pop and holds both scenes for the post-commit phase, which the motion plays from exactly where the gesture left them.

When activityBack is false at the start of a gesture, the gesture is passed through untouched and seeks predictivePopTransitionSpec instead, which suits a move between tabs. A back that is not a gesture and reports no progress, such as a key, plays popTransitionSpec.

backEnabled false keeps the display from handling back at all. A display inside a scene that is being covered, such as the tabs under a page that is still opening, is composed for the length of the transition; without it, its handler, registered after the outer display's, would take a back gesture meant for the page.

Handlers composed inside the display, such as an inner display or a drawer, still use the system dispatcher, and win over this one while they are enabled.

Feeding a dispatcher through DirectNavigationEventInput is the technique of 3 unique predictive back animations you can create with the Navigation Events library (tunjid.com).