agentleFS
PL-Coding-GmbHPL-Coding-GmbH/Skills/skills/kmp-navigation/SKILL.md

kmp-navigation

Skill18 starsChanged 4 months agoMITView on GitHub

Type-safe Compose Navigation for Kotlin Multiplatform (Android + iOS) - route objects, feature nav graphs, cross-feature callbacks, and wiring in :composeApp. Use this skill whenever setting up navigation, defining routes, adding a new screen to a nav graph, navigating between features, or wiring nav graphs in the app module. Trigger on phrases like "set up navigation", "add a route", "navigate between screens", "nav graph", "NavController", "type-safe nav", "cross-feature navigation", or "NavGraphBuilder".

2 other projects use a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

5 rules. About 0 are common, 2 are distinctive, and 3 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈2

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 3

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Use the multiplatform navigation artifact in commonMain (added by the cmp.feature convention plugin):

    line 21
  2. Very specific:

    Do not use androidx.navigation:navigation-compose in shared code — it is Android-only.

    line 27
  3. Very specific:

    Use data object for screens with no parameters, data class for screens with arguments.

    line 41
  4. Distinctive:

    All feature nav graphs are assembled in one place:

    line 76
  5. Distinctive:

    Avoid passing complex objects via navigation — pass IDs and load data in the destination ViewModel.

    line 111

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

KMP Navigation

Principles

  • Type-safe navigation with @Serializable route objects (KotlinX Serialization).
  • One nav graph per feature, defined in the feature's presentation module.
  • Feature nav graphs are assembled in :composeApp (commonMain), inside the shared App() composable.
  • Navigation within a feature uses a NavController passed into the feature nav graph.
  • Feature-to-feature navigation uses callbacks, keeping features decoupled.

Dependency

Use the multiplatform navigation artifact in commonMain (added by the cmp.feature convention plugin):

org.jetbrains.androidx.navigation:navigation-compose

Do not use androidx.navigation:navigation-compose in shared code — it is Android-only. Imports are unchanged (androidx.navigation.NavController, androidx.navigation.NavGraphBuilder, androidx.navigation.compose.NavHost, androidx.navigation.compose.composable, androidx.navigation.compose.navigation, androidx.navigation.toRoute). Every module that declares routes needs the KotlinX Serialization plugin (applied by kmp.library).


Route Objects

Define routes as @Serializable objects or data classes in the feature's presentation module:

// feature:notes:presentation
@Serializable object NoteListRoute
@Serializable data class NoteDetailRoute(val noteId: String)

Use data object for screens with no parameters, data class for screens with arguments.


Feature Nav Graph

Each feature exposes a NavGraphBuilder extension function:

// feature:notes:presentation
fun NavGraphBuilder.notesGraph(
    navController: NavController,
    onNavigateToEditor: (String) -> Unit  // callback for cross-feature navigation
) {
    navigation<NoteListRoute>(startDestination = NoteListRoute) {
        composable<NoteListRoute> {
            NoteListRoot(
                onNavigateToDetail = { navController.navigate(NoteDetailRoute(it)) }
            )
        }
        composable<NoteDetailRoute> { backStackEntry ->
            val route: NoteDetailRoute = backStackEntry.toRoute()
            NoteDetailRoot(
                noteId = route.noteId,
                onNavigateToEditor = onNavigateToEditor
            )
        }
    }
}

Wiring in :composeApp

All feature nav graphs are assembled in one place:

// :composeApp — commonMain, inside App()
val navController = rememberNavController()
NavHost(navController, startDestination = NoteListRoute) {
    notesGraph(
        navController = navController,
        onNavigateToEditor = { navController.navigate(EditorRoute(it)) }
    )
    editorGraph(navController)
}

Cross-feature navigation is always expressed as a lambda callback — never by importing a route from another feature module.


Passing Arguments

For simple scalar arguments, use @Serializable data class routes:

@Serializable data class NoteDetailRoute(val noteId: String)

// Navigate
navController.navigate(NoteDetailRoute(noteId = "abc123"))

// Receive
composable<NoteDetailRoute> { backStackEntry ->
    val route: NoteDetailRoute = backStackEntry.toRoute()
    // route.noteId available here
}

Avoid passing complex objects via navigation — pass IDs and load data in the destination ViewModel.


Platform Behavior

  • Back navigation: Android system back and predictive back work as usual. On iOS the edge swipe-back gesture is handled by the navigation library — no extra code. iOS has no system back button, so every non-root screen needs a visible back button in its top bar.
  • Intercepting back: androidx.activity.compose.BackHandler is not available in commonMain. Use the multiplatform back handler from org.jetbrains.compose.ui:ui-backhandler.
  • Deep links: routes are shared; the incoming URL arrives per platform (Intent in :androidApp, onOpenURL in the iOS app) and is forwarded to the shared NavController.
  • :androidApp contains no navigation code — it only hosts App().

Naming Conventions

ThingConventionExample
Nav route<Screen>RouteNoteListRoute, NoteDetailRoute
Feature nav graph<feature>Graph(...) on NavGraphBuildernotesGraph(...)

Checklist: Adding Navigation to a New Feature

  • Define @Serializable route objects for each screen in feature:presentation
  • Add feature nav graph function (NavGraphBuilder.<feature>Graph(...))
  • Pass NavController for intra-feature navigation
  • Expose cross-feature destinations as lambda callbacks (not direct route imports)
  • Wire nav graph and cross-feature callbacks in :composeApp's NavHost
  • Every non-root screen has a visible back button (iOS has no system back)

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.