Claude Cursor Skill

compose-multiplatform-navigation

Use when building or debugging navigation in Compose Multiplatform commonMain — Navigation 3 (NavDisplay, NavKey, back stack you own) or navigation-compose (NavHost, typed @Serializable routes, nested graphs), splash/login gate, session expiry, bottom tabs with per-tab back stack

LLM Mart · 0 points · 0 views 0 listing impressions 0 install-command copies
Download kushpawan-kmp-mobile-skills-skills_compose-multiplatform-navigation-c04c11f.zip · 22 KB

Install

skills CLI npx skills add https://github.com/Kushpawan/kmp-mobile-skills/tree/main/skills/compose-multiplatform-navigation
Claude Code claude plugin marketplace add https://llmmart.ai/marketplace.json && claude plugin install kushpawan-kmp-mobile-skills@llmmart
Git git clone https://github.com/Kushpawan/kmp-mobile-skills.git

The skills CLI installs just this skill, for any of its supported agents. Claude Code installs the whole kushpawan/kmp-mobile-skills collection as a plugin from our marketplace. Git is the plain clone.

Skill manifest

Compose Multiplatform navigation

One owner of each back stack (a NavController per NavHost, or one Navigation 3 back-stack list per NavDisplay), typed @Serializable routes, each feature registering its own destinations, and every navigation call made from one place per screen (its effect handler) or from the app shell. Session, tabs and deep links are app-shell concerns, not screen concerns.

When to use

  • Adding destinations, routes or a nested graph to a Compose Multiplatform app.
  • Building the app shell: splash → signed-out graph vs signed-in graph, reacting to session expiry.
  • Bottom navigation that keeps each tab's back stack.
  • Deep links (custom scheme, App Links, Universal Links) into shared navigation.
  • Intercepting back (unsaved changes) on Android and the iOS swipe-back gesture.
  • Passing a result back to the previous screen.
  • Symptoms: back stack resets after rotation, tab switch loses scroll/stack, a deep link opens a screen before login or crashes with IllegalArgumentException, a transition fires on the wrong edge, navigation happens twice.

When not to use

  • The screen itself (state, ViewModel, effects, tests) → compose-multiplatform-screen.
  • Android-only app on Jetpack Navigation 3 → Google's navigation-3 skill. Back handling inside an Android-only host activity → Google's navigation-event skill.
  • Native SwiftUI navigation (NavigationStack) over shared logic → SwiftUI skills in Related.
  • Projects on Voyager, Decompose, Appyx or another navigation library: follow that library; only the session/deep-link principles here transfer.

Step 0 — Read the project first

  1. Library and version. In gradle/libs.versions.toml find org.jetbrains.androidx.navigation:navigation-compose (Navigation 2 API), org.jetbrains.androidx.navigation3:* (Navigation 3), or another library. Typed routes need navigation-compose 2.8 or newer. Note the Compose Multiplatform version: it decides the back-handler API (step 8) and which navigation versions match (its CHANGELOG "Libraries" table; Compose Multiplatform 1.13 lists navigation-compose 2.10.0, Navigation3 1.2.0 and Navigation Event 1.1.0).

  2. Which library for this change — decide before writing code:

    Project state Use
    Existing navigation-compose graph Keep navigation-compose; steps 1–9. Do not add Navigation 3 beside it.
    Existing Navigation 3 back stack Keep Navigation 3; step 10.
    No navigation yet (greenfield), Compose Multiplatform 1.13+ Navigation 3, step 10, unless the team picks navigation-compose.
    Voyager, Decompose, Appyx, other That library (see When not to use).

    Moving an existing graph to Navigation 3 is a rewrite, not an upgrade: plan it as its own change.

  3. Route style. Typed @Serializable routes or string routes? Do not mix; if the project uses strings, ask before migrating.

  4. Host shape. Find every NavHost( and NavDisplay(. Is there one root host? A nested host for tabs? Where do features register destinations (fun NavGraphBuilder.xxx(...), or entry<Key> { } blocks in an entryProvider)?

  5. Session model. How the app knows it is signed in (token store, session repository, a Flow) and how expiry is signalled (401 → refresh failed → event).

  6. Transitions. Are enterTransition/exitTransition set on NavHost, on individual composable<...>() calls, or nowhere?

  7. Platform entry points. Android MainActivity (launchMode, onNewIntent, intent filters, any onBackPressed override, targetSdk); iOS entry (ComposeUIViewController in a SwiftUI App or a UIKit AppDelegate/SceneDelegate), Info.plist URL types, Associated Domains entitlement.

  8. Screen ↔ navigation contract. Do screens receive lambdas, a NavController, or a navigator interface? Keep whatever exists.

Workflow

Steps 1–9 are for navigation-compose (NavHost). For Navigation 3 (NavDisplay) go to step 10; the session, deep-link and back rules in steps 4, 7 and 8 still apply.

1. Define routes

@Serializable data object NotesListRoute
@Serializable data class NoteDetailRoute(val noteId: String)
@Serializable data class NoteEditorRoute(val noteId: String? = null)   // optional arg = default value
@Serializable data object NotesGraph                                   // nested graph key
  • Pass ids, not objects. Arguments are serialised into the back stack's saved state; large or sensitive payloads do not belong there.
  • Primitives, String, enums and lists of those work out of the box. Any other type needs a NavType passed through typeMap on composable<T>(typeMap = ...) and on toRoute<T>() callers — prefer an id instead.
  • Read arguments with backStackEntry.toRoute<NoteDetailRoute>(); in a ViewModel with SavedStateHandle.toRoute<T>(), or pass them as DI parameters.

Done when every destination has a route type and no string route literal remains in new code.

2. Let each feature own its graph; keep the root host flat

fun NavGraphBuilder.notesGraph(navController: NavController) {
    navigation<NotesGraph>(startDestination = NotesListRoute) {
        composable<NotesListRoute> { NotesListScreen(onOpenNote = { id -> navController.navigate(NoteDetailRoute(id)) }) }
        composable<NoteDetailRoute> { NoteDetailScreen(onBack = navController::popBackStack) }
    }
}

The root NavHost { } then lists one line per feature. Routes live next to the feature (or in its logic module if the project separates Compose-free code).

3. Configure transitions once, on the NavHost

  • Default: set none. Compose Multiplatform then gives iOS its native-like swipe-back animation; per the JetBrains docs, specifying enterTransition/exitTransition on NavHost turns that default off.
  • If the design needs custom transitions: set all four on NavHost only. Do not set them on individual composable<Route>(...) calls: popEnterTransition/popExitTransition default to the enterTransition/exitTransition you pass there, so a fade added for one edge silently applies to every push and pop touching that destination.
  • Need a special case (splash hand-off)? Branch on initialState.destination / targetState.destination inside the single NavHost-level lambda.

4. Gate the app on the session

Shape: SplashRoute (start) → AuthGraph (signed out) or MainRoute (signed in). An app-shell ViewModel observes the session and emits effects for the transitions; the root composable handles them by replacing the whole back stack:

fun NavController.navigateClearingBackStack(route: Any) = navigate(route) {
    popUpTo(graph.id) { inclusive = true }   // clear everything, including the start destination
    launchSingleTop = true
}
  • Startup, sign-in, sign-out and session expiry are all session changes → one place handles them.
  • Drive transitions from one-shot effects (or guard a state observer by checking the current graph). A bare LaunchedEffect(sessionState) { navigateClearingBackStack(...) } re-runs when the composition is recreated (Android configuration change) and wipes the back stack the NavController just restored.
  • Keep the OS splash up while the session is unknown (Android installSplashScreen() + setKeepOnScreenCondition) instead of flashing the sign-in screen.
  • Session expiry: show the "signed out" message from shell state, then navigate to AuthGraph. Remember a pending deep link and replay it after sign-in.

Full shell code (routes, ViewModel, root host, tests): references/app-shell-session-tabs.md.

5. Bottom tabs with a back stack per tab

  • Host the tabs in a nested NavHost inside composable<MainRoute>, with one nested graph per tab. The auth gate then never interferes with the tab host's start destination.
  • Switch tabs with the multiple-back-stacks recipe:
fun NavController.switchTab(tabGraph: Any) = navigate(tabGraph) {
    popUpTo(graph.findStartDestination().id) { saveState = true }
    launchSingleTop = true
    restoreState = true
}
  • Derive the selected tab from currentBackStackEntryAsState() + destination.hierarchy.any { it.hasRoute(tab.graph::class) } — never a separate var selectedTab that can drift from the back stack.
  • Re-selecting the current tab: pop that tab to its root (popBackStack(tabRootRoute, inclusive = false)).
  • Full-screen flows that should cover the bar (editor, checkout) go on the root controller.

6. Share a ViewModel across a nested graph

composable<CheckoutPaymentRoute> { entry ->
    val graphEntry = remember(entry) { navController.getBackStackEntry<CheckoutGraph>() }
    val checkoutViewModel: CheckoutViewModel = koinViewModel(viewModelStoreOwner = graphEntry)
}

The ViewModel lives as long as the graph is on the back stack. Without Koin use viewModel(viewModelStoreOwner = graphEntry) { ... }.

7. Deep links

Two shapes — pick by whether the target needs a signed-in user:

Shape How Use when
Declarative composable<NoteDetailRoute>(deepLinks = listOf(navDeepLink<NoteDetailRoute>(basePath = "example://notes/note"))) + navController.navigate(NavUri(uri)) Destination is reachable without the auth gate
Gated Parse the URI in commonMain into a route, hand it to the shell ViewModel, navigate after the session is known Anything behind sign-in
  • Check navController.graph.hasDeepLink(NavUri(uri)) first: navigate(NavUri) throws IllegalArgumentException for a URI no destination matches.
  • Android: intent filters in the manifest. iOS: CFBundleURLTypes (custom scheme) or Associated Domains (Universal Links), then forward the URL from Swift into a shared handler.
  • In a SwiftUI App (scene-based lifecycle) use .onOpenURL; application(_:open:options:) in an app delegate is not called when the app uses scenes.

Platform code, the shared URI handler, testing commands: references/deep-links.md.

8. Back handling

  • NavHost already maps Android system back and the iOS swipe-back to popBackStack(); NavDisplay maps them to its onBack.
  • Android 16, targetSdk 36+: predictive back animations are on by default, and Activity.onBackPressed() is no longer called and KEYCODE_BACK is no longer dispatched. Move any onBackPressed override in androidMain to a navigation-event handler. The manifest opt-out android:enableOnBackInvokedCallback="false" is temporary (Android 16 behavior changes).
  • To intercept (unsaved changes), on Compose Multiplatform 1.10+ use NavigationBackHandler from org.jetbrains.androidx.navigationevent:navigationevent-compose. On 1.8–1.9, use the multiplatform BackHandler (org.jetbrains.compose.ui:ui-backhandler), which later releases deprecate; follow the IDE's deprecation message when upgrading. If the project already uses BackHandler on 1.10+, keep it until a planned migration, then check each screen with a predictive back gesture on Android 16 and the iOS swipe.
  • Enable interception only while needed (isBackEnabled = state.hasUnsavedChanges) and send an action to the ViewModel; never pop inside the handler and again in an effect.
  • Disable the iOS swipe gesture app-wide only if required: ComposeUIViewController(configure = { enableBackGesture = false }) { App() }.

Code: references/back-and-results.md.

9. Return a result to the previous screen

Prefer, in order: (1) the data layer — the editor saves, the list observes a Flow and updates itself; (2) a graph-scoped shared ViewModel; (3) previousBackStackEntry?.savedStateHandle for a small primitive result, read with getStateFlow and cleared after it is consumed. Code in references/back-and-results.md.

10. Navigation 3 (new graphs)

Navigation 3 is multiplatform since Compose Multiplatform 1.10 and stable at 1.2.0 with Compose Multiplatform 1.13 (org.jetbrains.androidx.navigation3:navigation3-ui, which brings androidx navigation3-runtime 1.2.0). The back stack is a list you own; NavDisplay renders its top.

  • Keys implement NavKey and are @Serializable. On iOS (non-JVM) register them for polymorphic serialization in a SavedStateConfiguration and pass it to rememberNavBackStack(config, StartKey); a sealed interface per feature keeps that one line (subclassesOfSealed<NotesKey>()).
  • Navigate by editing the list: backStack.add(NoteDetailKey(id)), backStack.removeLastOrNull(). Only the app shell and effect handlers touch it, exactly as with a NavController.
  • Add rememberViewModelStoreNavEntryDecorator() (lifecycle-viewmodel-navigation3) next to rememberSaveableStateHolderNavEntryDecorator() so koinViewModel() is scoped to the entry and cleared on pop.
  • Session gate: switch between separate back stacks (or replace the list's contents) from shell effects; tabs keep one back stack per tab.
  • Deep links: Navigation3 1.2 adds multiplatform matching in androidx.navigation3.runtime.deeplink (DeepLinkRequest, DeepLinkUri, UriDeepLinkMatcher); keep the gated shape.
  • iOS: since Compose Multiplatform 1.13 the default push/pop transitions follow layout direction (RTL).

Setup, root display, gate, tabs and back: references/navigation-3.md. Deep-link matching: section 8 of references/deep-links.md.

Quick reference

Task API
Typed destination composable<Route> { entry -> entry.toRoute<Route>() }
Nested graph navigation<GraphKey>(startDestination = FirstRoute) { ... }
Clear stack and go navigate(route) { popUpTo(graph.id) { inclusive = true }; launchSingleTop = true }
Pop to a typed route popBackStack<NotesListRoute>(inclusive = false)
Current destination check destination.hierarchy.any { it.hasRoute(NotesGraph::class) }
Observe current entry navController.currentBackStackEntryAsState()
Graph-scoped owner navController.getBackStackEntry<GraphKey>()
Deep link pattern navDeepLink<NoteDetailRoute>(basePath = "example://notes/note") matches example://notes/note/42
Navigate by URI navController.navigate(NavUri(uri)) (androidx.navigation.NavUri)
Back interception, CMP 1.10+ NavigationBackHandler(state = rememberNavigationEventState(...), isBackEnabled, onBackCompleted)
Disable iOS swipe-back ComposeUIViewController(configure = { enableBackGesture = false })
Nav3 back stack (iOS-safe) rememberNavBackStack(savedStateConfiguration, StartKey)
Nav3 display NavDisplay(backStack, onBack = { backStack.removeLastOrNull() }, entryDecorators = …, entryProvider = entryProvider { entry<Key> { key -> … } })
Nav3 deep-link match UriDeepLinkMatcher(DeepLinkUri("example://notes/note/{noteId}"), NoteDetailKey.serializer()).match(DeepLinkRequest(uri))?.key

Verify

Report every check as PASS, FAIL or NOT RUN (reason); never claim one you did not run.

./gradlew :composeApp:compileKotlinMetadata                 # routes + graphs compile in common code
./gradlew :composeApp:assembleDebug                         # Android build
./gradlew :shared:testAndroidHostTest                       # shell ViewModel / deep-link parser tests (or testDebugUnitTest)
./gradlew :shared:iosSimulatorArm64Test                     # same tests on iOS
adb shell am start -W -a android.intent.action.VIEW -d "example://notes/note/42" <applicationId>
xcrun simctl openurl booted "example://notes/note/42"

Manual run on both platforms (report NOT RUN if no device/simulator):

  1. Cold start signed out → sign-in; sign in → main tabs; back from the first tab exits the app.
  2. Rotate (Android) on a deep screen → the back stack is unchanged.
  3. Tab A → push detail → tab B → tab A: the detail is still there.
  4. Expire the session → message, then sign-in; after sign-in the pending deep link opens.
  5. Deep link while signed out → sign-in first, then the target. Unknown URI → no crash.
  6. iOS swipe-back pops one screen; unsaved-changes guard intercepts it.
  7. Android 16 with targetSdk 36+: the predictive back gesture previews the previous screen and the unsaved-changes guard still intercepts it.

Common mistakes

Mistake Why it bites Fix
enterTransition/exitTransition on one composable<Route>() Pop transitions default to them: the effect leaks onto unrelated push/pop edges Configure transitions only on NavHost
Custom NavHost transitions without checking iOS Disables the default iOS swipe-back animation Leave defaults, or accept and test the custom feel on iOS
LaunchedEffect(session) { navigateClearingBackStack(...) } Re-runs after recreation and wipes the restored stack Effect-driven transitions, or guard on the current graph
Tabs as a var selectedTab + when Loses each tab's stack and state; drifts from back stack Nested tab NavHost + saveState/restoreState; derive selection
popUpTo(graph.findStartDestination().id) on a root whose start is the splash Start destination is no longer on the stack; tab stacks accumulate Put tabs in their own nested host
navigate(NavUri(uri)) without hasDeepLink IllegalArgumentException on unknown links Check first, fall back to home
Declarative deep link to a gated screen Opens before session resolution, skipping sign-in Gated shape: parse → pending → navigate after sign-in
Forwarding the Android launch intent and letting NavHost handle it The AndroidX NavController already handles the launching intent's deep link; the screen opens twice Forward only onNewIntent, or use the gated shape end-to-end
application(_:open:options:) in a SwiftUI App Not called with the scene lifecycle .onOpenURL { }
Passing whole objects as route arguments Saved-state bloat, custom NavType boilerplate, stale data Pass the id; load in the destination
Navigating from a ViewModel Leaks the controller; untestable Effect → effect handler / shell
Back handler always enabled Swallows back on every screen state isBackEnabled bound to the state that needs it
savedStateHandle result never cleared Re-applied on every recomposition or return Reset the key after consuming
onBackPressed() override in the Android activity Not called at targetSdk 36 on Android 16+; the guard silently stops working Navigation-event handler in shared code
Navigation 3 and navigation-compose in one host Two back-stack owners; back and deep links disagree One library per host; migrate a graph as its own change
rememberNavBackStack(StartKey) without a SavedStateConfiguration That overload needs reflection serialization, which iOS lacks Register keys polymorphically and pass the configuration
Nav3 without the ViewModel entry decorator ViewModels outlive the popped entry or are shared between entries rememberViewModelStoreNavEntryDecorator()

Related skills

Files (kmp-mobile-skills)
  • references
    • app-shell-session-tabs.md 13.9 KB
      Loading…
    • back-and-results.md 6 KB
      Loading…
    • deep-links.md 10.1 KB
      Loading…
    • navigation-3.md 7.5 KB
      Loading…
  • SKILL.md 21 KB
    ---
    name: compose-multiplatform-navigation
    description: Use when building or debugging navigation in Compose Multiplatform commonMain — Navigation 3 (NavDisplay, NavKey, back stack you own) or navigation-compose (NavHost, typed @Serializable routes, nested graphs), splash/login gate, session expiry, bottom tabs with per-tab back stacks, deep links on Android and iOS, predictive back, iOS swipe-back, returning results. Also for back stacks resetting, tabs losing state, double navigation, deep links that crash or skip login. Not for Android-only Navigation 3 apps (Google's navigation-3 skill).
    license: Apache-2.0
    compatibility: Kotlin Multiplatform project using Compose Multiplatform 1.8+ with the multiplatform navigation-compose artifact (org.jetbrains.androidx.navigation), or Compose Multiplatform 1.10+ with Navigation 3 (org.jetbrains.androidx.navigation3). iOS steps need macOS with Xcode.
    metadata:
      category: ui
      platforms: android, ios
    ---
    
    # Compose Multiplatform navigation
    
    One owner of each back stack (a `NavController` per `NavHost`, or one Navigation 3 back-stack list
    per `NavDisplay`), typed `@Serializable` routes, each feature registering its own destinations, and
    every navigation call made from one place per screen (its effect handler) or from the app shell.
    Session, tabs and deep links are app-shell concerns, not screen concerns.
    
    ## When to use
    
    - Adding destinations, routes or a nested graph to a Compose Multiplatform app.
    - Building the app shell: splash → signed-out graph vs signed-in graph, reacting to session expiry.
    - Bottom navigation that keeps each tab's back stack.
    - Deep links (custom scheme, App Links, Universal Links) into shared navigation.
    - Intercepting back (unsaved changes) on Android and the iOS swipe-back gesture.
    - Passing a result back to the previous screen.
    - Symptoms: back stack resets after rotation, tab switch loses scroll/stack, a deep link opens a
      screen before login or crashes with `IllegalArgumentException`, a transition fires on the wrong
      edge, navigation happens twice.
    
    ## When not to use
    
    - The screen itself (state, ViewModel, effects, tests) → `compose-multiplatform-screen`.
    - Android-only app on Jetpack Navigation 3 → Google's
      [navigation-3 skill](https://github.com/android/skills/tree/main/navigation/navigation-3).
      Back handling inside an Android-only host activity → Google's
      [navigation-event skill](https://github.com/android/skills/tree/main/navigation/navigation-event).
    - Native SwiftUI navigation (`NavigationStack`) over shared logic → SwiftUI skills in Related.
    - Projects on Voyager, Decompose, Appyx or another navigation library: follow that library; only the
      session/deep-link principles here transfer.
    
    ## Step 0 — Read the project first
    
    1. **Library and version.** In `gradle/libs.versions.toml` find
       `org.jetbrains.androidx.navigation:navigation-compose` (Navigation 2 API),
       `org.jetbrains.androidx.navigation3:*` (Navigation 3), or another library. Typed routes need
       navigation-compose **2.8 or newer**. Note the Compose Multiplatform version: it decides the
       back-handler API (step 8) and which navigation versions match (its CHANGELOG "Libraries" table;
       Compose Multiplatform 1.13 lists navigation-compose 2.10.0, Navigation3 1.2.0 and Navigation
       Event 1.1.0).
    2. **Which library for this change** — decide before writing code:
    
       | Project state | Use |
       |---|---|
       | Existing navigation-compose graph | Keep navigation-compose; steps 1–9. Do not add Navigation 3 beside it. |
       | Existing Navigation 3 back stack | Keep Navigation 3; step 10. |
       | No navigation yet (greenfield), Compose Multiplatform 1.13+ | Navigation 3, step 10, unless the team picks navigation-compose. |
       | Voyager, Decompose, Appyx, other | That library (see When not to use). |
    
       Moving an existing graph to Navigation 3 is a rewrite, not an upgrade: plan it as its own change.
    3. **Route style.** Typed `@Serializable` routes or string routes? Do not mix; if the project uses
       strings, ask before migrating.
    4. **Host shape.** Find every `NavHost(` and `NavDisplay(`. Is there one root host? A nested host
       for tabs? Where do features register destinations (`fun NavGraphBuilder.xxx(...)`, or
       `entry<Key> { }` blocks in an `entryProvider`)?
    5. **Session model.** How the app knows it is signed in (token store, session repository, a
       `Flow`) and how expiry is signalled (401 → refresh failed → event).
    6. **Transitions.** Are `enterTransition`/`exitTransition` set on `NavHost`, on individual
       `composable<...>()` calls, or nowhere?
    7. **Platform entry points.** Android `MainActivity` (`launchMode`, `onNewIntent`, intent filters,
       any `onBackPressed` override, `targetSdk`);
       iOS entry (`ComposeUIViewController` in a SwiftUI `App` or a UIKit `AppDelegate`/`SceneDelegate`),
       `Info.plist` URL types, Associated Domains entitlement.
    8. **Screen ↔ navigation contract.** Do screens receive lambdas, a `NavController`, or a navigator
       interface? Keep whatever exists.
    
    ## Workflow
    
    Steps 1–9 are for navigation-compose (`NavHost`). For Navigation 3 (`NavDisplay`) go to step 10;
    the session, deep-link and back rules in steps 4, 7 and 8 still apply.
    
    ### 1. Define routes
    
    ```kotlin
    @Serializable data object NotesListRoute
    @Serializable data class NoteDetailRoute(val noteId: String)
    @Serializable data class NoteEditorRoute(val noteId: String? = null)   // optional arg = default value
    @Serializable data object NotesGraph                                   // nested graph key
    ```
    
    - Pass **ids**, not objects. Arguments are serialised into the back stack's saved state; large or
      sensitive payloads do not belong there.
    - Primitives, `String`, enums and lists of those work out of the box. Any other type needs a
      `NavType` passed through `typeMap` on `composable<T>(typeMap = ...)` **and** on `toRoute<T>()`
      callers — prefer an id instead.
    - Read arguments with `backStackEntry.toRoute<NoteDetailRoute>()`; in a ViewModel with
      `SavedStateHandle.toRoute<T>()`, or pass them as DI parameters.
    
    Done when every destination has a route type and no string route literal remains in new code.
    
    ### 2. Let each feature own its graph; keep the root host flat
    
    ```kotlin
    fun NavGraphBuilder.notesGraph(navController: NavController) {
        navigation<NotesGraph>(startDestination = NotesListRoute) {
            composable<NotesListRoute> { NotesListScreen(onOpenNote = { id -> navController.navigate(NoteDetailRoute(id)) }) }
            composable<NoteDetailRoute> { NoteDetailScreen(onBack = navController::popBackStack) }
        }
    }
    ```
    
    The root `NavHost { }` then lists one line per feature. Routes live next to the feature (or in its
    logic module if the project separates Compose-free code).
    
    ### 3. Configure transitions once, on the `NavHost`
    
    - **Default:** set none. Compose Multiplatform then gives iOS its native-like swipe-back animation;
      per the JetBrains docs, specifying `enterTransition`/`exitTransition` on `NavHost` turns that
      default off.
    - **If the design needs custom transitions:** set all four on `NavHost` only. Do **not** set them on
      individual `composable<Route>(...)` calls: `popEnterTransition`/`popExitTransition` default to the
      `enterTransition`/`exitTransition` you pass there, so a fade added for one edge silently applies to
      every push and pop touching that destination.
    - Need a special case (splash hand-off)? Branch on `initialState.destination` /
      `targetState.destination` inside the single `NavHost`-level lambda.
    
    ### 4. Gate the app on the session
    
    Shape: `SplashRoute` (start) → `AuthGraph` (signed out) or `MainRoute` (signed in). An app-shell
    ViewModel observes the session and emits **effects** for the transitions; the root composable
    handles them by replacing the whole back stack:
    
    ```kotlin
    fun NavController.navigateClearingBackStack(route: Any) = navigate(route) {
        popUpTo(graph.id) { inclusive = true }   // clear everything, including the start destination
        launchSingleTop = true
    }
    ```
    
    - Startup, sign-in, sign-out and session expiry are all session changes → one place handles them.
    - Drive transitions from one-shot effects (or guard a state observer by checking the current graph).
      A bare `LaunchedEffect(sessionState) { navigateClearingBackStack(...) }` re-runs when the composition is
      recreated (Android configuration change) and wipes the back stack the `NavController` just
      restored.
    - Keep the OS splash up while the session is unknown (Android `installSplashScreen()` +
      `setKeepOnScreenCondition`) instead of flashing the sign-in screen.
    - Session expiry: show the "signed out" message from shell state, then navigate to `AuthGraph`.
      Remember a pending deep link and replay it after sign-in.
    
    Full shell code (routes, ViewModel, root host, tests):
    [references/app-shell-session-tabs.md](references/app-shell-session-tabs.md).
    
    ### 5. Bottom tabs with a back stack per tab
    
    - Host the tabs in a **nested `NavHost`** inside `composable<MainRoute>`, with one nested graph per
      tab. The auth gate then never interferes with the tab host's start destination.
    - Switch tabs with the multiple-back-stacks recipe:
    
    ```kotlin
    fun NavController.switchTab(tabGraph: Any) = navigate(tabGraph) {
        popUpTo(graph.findStartDestination().id) { saveState = true }
        launchSingleTop = true
        restoreState = true
    }
    ```
    
    - Derive the selected tab from `currentBackStackEntryAsState()` +
      `destination.hierarchy.any { it.hasRoute(tab.graph::class) }` — never a separate `var selectedTab`
      that can drift from the back stack.
    - Re-selecting the current tab: pop that tab to its root (`popBackStack(tabRootRoute, inclusive = false)`).
    - Full-screen flows that should cover the bar (editor, checkout) go on the **root** controller.
    
    ### 6. Share a ViewModel across a nested graph
    
    ```kotlin
    composable<CheckoutPaymentRoute> { entry ->
        val graphEntry = remember(entry) { navController.getBackStackEntry<CheckoutGraph>() }
        val checkoutViewModel: CheckoutViewModel = koinViewModel(viewModelStoreOwner = graphEntry)
    }
    ```
    
    The ViewModel lives as long as the graph is on the back stack. Without Koin use
    `viewModel(viewModelStoreOwner = graphEntry) { ... }`.
    
    ### 7. Deep links
    
    Two shapes — pick by whether the target needs a signed-in user:
    
    | Shape | How | Use when |
    |---|---|---|
    | Declarative | `composable<NoteDetailRoute>(deepLinks = listOf(navDeepLink<NoteDetailRoute>(basePath = "example://notes/note")))` + `navController.navigate(NavUri(uri))` | Destination is reachable without the auth gate |
    | Gated | Parse the URI in `commonMain` into a route, hand it to the shell ViewModel, navigate after the session is known | Anything behind sign-in |
    
    - Check `navController.graph.hasDeepLink(NavUri(uri))` first: `navigate(NavUri)` throws
      `IllegalArgumentException` for a URI no destination matches.
    - Android: intent filters in the manifest. iOS: `CFBundleURLTypes` (custom scheme) or Associated
      Domains (Universal Links), then forward the URL from Swift into a shared handler.
    - In a SwiftUI `App` (scene-based lifecycle) use `.onOpenURL`; `application(_:open:options:)` in an
      app delegate is not called when the app uses scenes.
    
    Platform code, the shared URI handler, testing commands:
    [references/deep-links.md](references/deep-links.md).
    
    ### 8. Back handling
    
    - `NavHost` already maps Android system back and the iOS swipe-back to `popBackStack()`;
      `NavDisplay` maps them to its `onBack`.
    - **Android 16, `targetSdk` 36+:** predictive back animations are on by default, and
      `Activity.onBackPressed()` is no longer called and `KEYCODE_BACK` is no longer dispatched. Move any
      `onBackPressed` override in `androidMain` to a navigation-event handler. The manifest opt-out
      `android:enableOnBackInvokedCallback="false"` is temporary
      ([Android 16 behavior changes](https://developer.android.com/about/versions/16/behavior-changes-16)).
    - To intercept (unsaved changes), on Compose Multiplatform **1.10+** use `NavigationBackHandler`
      from `org.jetbrains.androidx.navigationevent:navigationevent-compose`. On **1.8–1.9**, use the
      multiplatform `BackHandler` (`org.jetbrains.compose.ui:ui-backhandler`), which later releases
      deprecate; follow the IDE's deprecation message when upgrading. If the project already uses
      `BackHandler` on 1.10+, keep it until a planned migration, then check each screen with a predictive
      back gesture on Android 16 and the iOS swipe.
    - Enable interception only while needed (`isBackEnabled = state.hasUnsavedChanges`) and send an
      action to the ViewModel; never pop inside the handler and again in an effect.
    - Disable the iOS swipe gesture app-wide only if required:
      `ComposeUIViewController(configure = { enableBackGesture = false }) { App() }`.
    
    Code: [references/back-and-results.md](references/back-and-results.md).
    
    ### 9. Return a result to the previous screen
    
    Prefer, in order: (1) the data layer — the editor saves, the list observes a `Flow` and updates
    itself; (2) a graph-scoped shared ViewModel; (3) `previousBackStackEntry?.savedStateHandle` for a
    small primitive result, read with `getStateFlow` and **cleared after it is consumed**.
    Code in [references/back-and-results.md](references/back-and-results.md).
    
    ### 10. Navigation 3 (new graphs)
    
    Navigation 3 is multiplatform since Compose Multiplatform 1.10 and stable at 1.2.0 with Compose
    Multiplatform 1.13 (`org.jetbrains.androidx.navigation3:navigation3-ui`, which brings androidx
    `navigation3-runtime` 1.2.0). The back stack is a list **you** own; `NavDisplay` renders its top.
    
    - Keys implement `NavKey` and are `@Serializable`. On iOS (non-JVM) register them for polymorphic
      serialization in a `SavedStateConfiguration` and pass it to `rememberNavBackStack(config, StartKey)`;
      a sealed interface per feature keeps that one line (`subclassesOfSealed<NotesKey>()`).
    - Navigate by editing the list: `backStack.add(NoteDetailKey(id))`, `backStack.removeLastOrNull()`.
      Only the app shell and effect handlers touch it, exactly as with a `NavController`.
    - Add `rememberViewModelStoreNavEntryDecorator()` (`lifecycle-viewmodel-navigation3`) next to
      `rememberSaveableStateHolderNavEntryDecorator()` so `koinViewModel()` is scoped to the entry and
      cleared on pop.
    - Session gate: switch between separate back stacks (or replace the list's contents) from shell
      effects; tabs keep one back stack per tab.
    - Deep links: Navigation3 1.2 adds multiplatform matching in `androidx.navigation3.runtime.deeplink`
      (`DeepLinkRequest`, `DeepLinkUri`, `UriDeepLinkMatcher`); keep the gated shape.
    - iOS: since Compose Multiplatform 1.13 the default push/pop transitions follow layout direction (RTL).
    
    Setup, root display, gate, tabs and back: [references/navigation-3.md](references/navigation-3.md).
    Deep-link matching: section 8 of [references/deep-links.md](references/deep-links.md).
    
    ## Quick reference
    
    | Task | API |
    |---|---|
    | Typed destination | `composable<Route> { entry -> entry.toRoute<Route>() }` |
    | Nested graph | `navigation<GraphKey>(startDestination = FirstRoute) { ... }` |
    | Clear stack and go | `navigate(route) { popUpTo(graph.id) { inclusive = true }; launchSingleTop = true }` |
    | Pop to a typed route | `popBackStack<NotesListRoute>(inclusive = false)` |
    | Current destination check | `destination.hierarchy.any { it.hasRoute(NotesGraph::class) }` |
    | Observe current entry | `navController.currentBackStackEntryAsState()` |
    | Graph-scoped owner | `navController.getBackStackEntry<GraphKey>()` |
    | Deep link pattern | `navDeepLink<NoteDetailRoute>(basePath = "example://notes/note")` matches `example://notes/note/42` |
    | Navigate by URI | `navController.navigate(NavUri(uri))` (`androidx.navigation.NavUri`) |
    | Back interception, CMP 1.10+ | `NavigationBackHandler(state = rememberNavigationEventState(...), isBackEnabled, onBackCompleted)` |
    | Disable iOS swipe-back | `ComposeUIViewController(configure = { enableBackGesture = false })` |
    | Nav3 back stack (iOS-safe) | `rememberNavBackStack(savedStateConfiguration, StartKey)` |
    | Nav3 display | `NavDisplay(backStack, onBack = { backStack.removeLastOrNull() }, entryDecorators = …, entryProvider = entryProvider { entry<Key> { key -> … } })` |
    | Nav3 deep-link match | `UriDeepLinkMatcher(DeepLinkUri("example://notes/note/{noteId}"), NoteDetailKey.serializer()).match(DeepLinkRequest(uri))?.key` |
    
    ## Verify
    
    Report every check as **PASS**, **FAIL** or **NOT RUN (reason)**; never claim one you did not run.
    
    ```bash
    ./gradlew :composeApp:compileKotlinMetadata                 # routes + graphs compile in common code
    ./gradlew :composeApp:assembleDebug                         # Android build
    ./gradlew :shared:testAndroidHostTest                       # shell ViewModel / deep-link parser tests (or testDebugUnitTest)
    ./gradlew :shared:iosSimulatorArm64Test                     # same tests on iOS
    adb shell am start -W -a android.intent.action.VIEW -d "example://notes/note/42" <applicationId>
    xcrun simctl openurl booted "example://notes/note/42"
    ```
    
    Manual run on both platforms (report NOT RUN if no device/simulator):
    
    1. Cold start signed out → sign-in; sign in → main tabs; back from the first tab exits the app.
    2. Rotate (Android) on a deep screen → the back stack is unchanged.
    3. Tab A → push detail → tab B → tab A: the detail is still there.
    4. Expire the session → message, then sign-in; after sign-in the pending deep link opens.
    5. Deep link while signed out → sign-in first, then the target. Unknown URI → no crash.
    6. iOS swipe-back pops one screen; unsaved-changes guard intercepts it.
    7. Android 16 with `targetSdk` 36+: the predictive back gesture previews the previous screen and the
       unsaved-changes guard still intercepts it.
    
    ## Common mistakes
    
    | Mistake | Why it bites | Fix |
    |---|---|---|
    | `enterTransition`/`exitTransition` on one `composable<Route>()` | Pop transitions default to them: the effect leaks onto unrelated push/pop edges | Configure transitions only on `NavHost` |
    | Custom `NavHost` transitions without checking iOS | Disables the default iOS swipe-back animation | Leave defaults, or accept and test the custom feel on iOS |
    | `LaunchedEffect(session) { navigateClearingBackStack(...) }` | Re-runs after recreation and wipes the restored stack | Effect-driven transitions, or guard on the current graph |
    | Tabs as a `var selectedTab` + `when` | Loses each tab's stack and state; drifts from back stack | Nested tab `NavHost` + `saveState`/`restoreState`; derive selection |
    | `popUpTo(graph.findStartDestination().id)` on a root whose start is the splash | Start destination is no longer on the stack; tab stacks accumulate | Put tabs in their own nested host |
    | `navigate(NavUri(uri))` without `hasDeepLink` | `IllegalArgumentException` on unknown links | Check first, fall back to home |
    | Declarative deep link to a gated screen | Opens before session resolution, skipping sign-in | Gated shape: parse → pending → navigate after sign-in |
    | Forwarding the Android launch intent **and** letting `NavHost` handle it | The AndroidX `NavController` already handles the launching intent's deep link; the screen opens twice | Forward only `onNewIntent`, or use the gated shape end-to-end |
    | `application(_:open:options:)` in a SwiftUI `App` | Not called with the scene lifecycle | `.onOpenURL { }` |
    | Passing whole objects as route arguments | Saved-state bloat, custom `NavType` boilerplate, stale data | Pass the id; load in the destination |
    | Navigating from a ViewModel | Leaks the controller; untestable | Effect → effect handler / shell |
    | Back handler always enabled | Swallows back on every screen state | `isBackEnabled` bound to the state that needs it |
    | `savedStateHandle` result never cleared | Re-applied on every recomposition or return | Reset the key after consuming |
    | `onBackPressed()` override in the Android activity | Not called at `targetSdk` 36 on Android 16+; the guard silently stops working | Navigation-event handler in shared code |
    | Navigation 3 and navigation-compose in one host | Two back-stack owners; back and deep links disagree | One library per host; migrate a graph as its own change |
    | `rememberNavBackStack(StartKey)` without a `SavedStateConfiguration` | That overload needs reflection serialization, which iOS lacks | Register keys polymorphically and pass the configuration |
    | Nav3 without the ViewModel entry decorator | ViewModels outlive the popped entry or are shared between entries | `rememberViewModelStoreNavEntryDecorator()` |
    
    ## Related skills
    
    - `compose-multiplatform-screen` — the screen contract that emits navigation effects.
    - `compose-multiplatform-localization` — localised labels for tabs and titles.
    - [android/skills — navigation-3](https://github.com/android/skills/tree/main/navigation/navigation-3)
      — Google's Navigation 3 skill for Android-only apps; its `modular-koin` recipe shows feature
      modules contributing entries through Koin.
    - [android/skills — navigation-event](https://github.com/android/skills/tree/main/navigation/navigation-event)
      — predictive back and `NavigationBackHandler` in Android-only code (host activity, `androidMain`).
    - [chrisbanes/skills](https://github.com/chrisbanes/skills) — `compose-state-and-effects`.
    - [AvdLee/SwiftUI-Agent-Skill](https://github.com/AvdLee/SwiftUI-Agent-Skill) and
      [twostraws/SwiftUI-Agent-Skill](https://github.com/twostraws/SwiftUI-Agent-Skill) — when iOS uses
      native SwiftUI navigation instead of shared Compose navigation.
    

Comments

Sign in to join the conversation.

No comments yet. Start the conversation.

Reviews (0)

No reviews yet.