Repository navigation
Migrate navigation graph design, Kotlin DSL, nested graph, and Navigation Editor snippets - #1221
Open
edward-williams-stellarelements wants to merge 1 commit into
Conversation
…tion Editor snippets
edward-williams-stellarelements
requested a review
from a team
as a code owner
October 23, 2026 20:56
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Code snippets are for:
This PR moves 15 code blocks from these four guide pages into compiled files. Each page gets its own package, so route names that repeat across pages don't collide.
android_compose_navigation_design_argumentscompose/.../navigation/design/DesignGraph.ktandroid_compose_navigation_design_optional_argumentscompose/.../navigation/design/DesignGraph.ktandroid_views_navigation_design_nav_graphviews/src/main/res/navigation/nav_graph_design.xmlandroid_views_navigation_kotlin_dsl_routesviews/.../navigation/kotlindsl/KotlinDsl.ktandroid_views_navigation_kotlin_dsl_navigateviews/.../navigation/kotlindsl/KotlinDsl.ktandroid_views_navigation_kotlin_dsl_custom_destinationviews/.../navigation/kotlindsl/KotlinDsl.ktandroid_views_navigation_kotlin_dsl_custom_destination_unary_plusviews/.../navigation/kotlindsl/KotlinDsl.ktandroid_views_navigation_kotlin_dsl_custom_type_navigateviews/.../navigation/kotlindsl/KotlinDsl.ktandroid_compose_navigation_nested_graphs_navigatecompose/.../navigation/nestedgraphs/NestedGraphs.ktandroid_views_navigation_nested_graphs_nested_graphviews/src/main/res/navigation/nav_graph_nested_graphs.xmlandroid_views_navigation_nested_graphs_navigate_actionviews/.../navigation/nestedgraphs/NestedGraphs.ktandroid_views_navigation_nested_graphs_includeviews/src/main/res/navigation/nav_graph_nested_graphs_include.xmlandroid_views_navigation_nested_graphs_included_graphviews/src/main/res/navigation/included_graph.xmlandroid_views_navigation_editor_empty_graphviews/src/main/res/navigation/nav_graph_editor_empty.xmlandroid_views_navigation_editor_destinationviews/src/main/res/navigation/nav_graph_editor_destination.xmlcompose/...iscompose/snippets/src/main/java/com/example/compose/snippets, andviews/...isviews/src/main/java/com/example/example/snippet/views.The two route classes on the design page and the
navigate()call on the nested-graphs page sit in the Compose sections of those pages, so they go in:compose:snippets. The Fragment, Kotlin DSL, and XML code goes in:views.Dependencies added to
:viewsThese are the first Fragment navigation snippets in
:views, so the module needs:androidx.navigation:navigation-fragment-ktx, as a newandroidx-navigation-fragment-ktxcatalog entry on the existingandroidx-navigationversion. Directly or through the Navigation runtime library it brings in, it provides:Fragment.findNavController()inandroid_views_navigation_kotlin_dsl_navigate.View.findNavController()inandroid_views_navigation_nested_graphs_navigate_action.NavController.navigatorProviderand the graph builder'saddDestination()and unary plus in the two custom destination snippets, andNavController.navigate()inandroid_views_navigation_kotlin_dsl_custom_type_navigate.<navigation>,<fragment>,<action>, and<include>elements and theirapp:startDestination,app:destination, andapp:graphattributes in the six graph XML files.kotlinx-serialization-core, for the@Serializableroute classes inandroid_views_navigation_kotlin_dsl_routesand the routes that the Kotlin-dsl navigate snippets pass tonavigate(). Navigation brings inkotlinx-serialization-coreonly at runtime, so the annotation needs the direct dependency.:compose:snippetsalready has Navigation Compose and Kotlin serialization, so it needs nothing new.Supporting code and resources
Everything in this section sits outside the region tags, so none of it shows on the guide pages.
Profiletwice, with different parameters, so each declaration sits in its ownprivate object(RouteWithArgumentandRouteWithOptionalArgument).navigateToPlant()callsfindNavController()with no receiver, which is theFragmentextension, so it sits in aHomeFragmentclass (the page's name for the fragment of theHomedestination). The class is public because the system recreates fragments by class name.PlantDetail(id), plusSearchParametersandSearchRoute, which mirror the page's declarations under "Providing custom types" (without@Parcelize, which compiling doesn't need).createGraph()builder in aNavControllerextension, sonavigatorProvideris the NavController's provider, as the snippets' comment says.CustomNavigatoris a minimalNavigator<NavDestination>, andGraph.CustomDestination.routeis a private string constant.navigateToSearch()takes theNavControllerthat the snippet calls.navigate()call gets itsNavControllerfrom a function parameter, plus a privateMatchroute object. The Fragment version takes theViewthat it callsfindNavController()on.Resources that the graph XML needs to build. The guide pages don't print them, but the build fails without them:
included_graph.xml: the root graph includes@navigation/included_graph, so the page's own code forces this file name. The file holds the page's second graph.label_blankinviews/src/main/res/values/strings.xml(valueBlank): the editor page's destination usesandroid:label="@string/label_blank".tools:layoutreferences (fragment_main,fragment_blank, and others) only affect the Navigation Editor preview, so no layout files are added for them.List of modifications (visible diffs vs. the page)
android_views_navigation_kotlin_dsl_custom_destinationandandroid_views_navigation_kotlin_dsl_custom_destination_unary_plus: the comment now ends with a full stop:// The NavigatorProvider is retrieved from the NavController.navigateToPlant().android_views_navigation_nested_graphs_nested_graph,android_views_navigation_nested_graphs_include, andandroid_views_navigation_nested_graphs_included_graph: the snippets start at the root<navigation>element, without the page's<?xml version="1.0" encoding="utf-8"?>line. An XML declaration has to be the first line of the file, ahead of the license header and the region tag, so it can't sit inside a region. (In the two include examples, the page prints it after a comment, where an XML parser rejects it.)android_views_navigation_nested_graphs_include: the page's...line is written as the XML comment<!-- ... -->, so the file is valid XML.navigate()snippets match the page.<navigation>element, without the<?xml version="1.0" encoding="utf-8"?>line, for the same reason.Snippets not migrated
The other code blocks on these pages are left as they are on the guide pages for now:
NavHostandcreateGraph()examples, the route-instance and minimal examples, the twoNavHostFragmentlayouts, and the Kotlin DSL graph).createGraph()graph, the back stack andSavedStateHandleroute reads, the fragment, activity, and navigation destination builders, the argument and custom type declarations, the customNavType, the typed fragment destination, the search route read, the deep link, and the URI format route).navigate()call isn't migrated. The guide page is being updated separately to show only the Kotlin version.Build:
./gradlew :views:spotlessApply :compose:snippets:spotlessApplyruns first. Then./gradlew :views:buildand./gradlew :compose:snippets:buildboth pass, with no warnings or lint findings in the added files.