Skip to content

Migrate activity and dialog destination, action, and NavController snippets - #1220

Open
edward-williams-stellarelements wants to merge 1 commit into
android:mainfrom
StellarElements:entcwilliams/navigation-destinations
Open

edward-williams-stellarelements wants to merge 1 commit into
android:mainfrom
StellarElements:entcwilliams/navigation-destinations

Conversation

@edward-williams-stellarelements

Copy link
Copy Markdown

Code snippets are for:

This PR adds 13 region tags, one for each code block it migrates from these pages: 12 in :views (8 navigation graphs and 4 Kotlin blocks) and 1 in :compose:snippets. Each page gets its own sub-package, and each navigation graph that the guide prints as a complete file gets its own file in views/src/main/res/navigation/.

  • Activity-destinations page (:views):
    • nav_graph_activity_destinations_class_name.xml: android_views_navigation_activity_destinations_class_name
    • nav_graph_activity_destinations_implicit_intent.xml: android_views_navigation_activity_destinations_implicit_intent
    • nav_graph_activity_destinations_other_app.xml: android_views_navigation_activity_destinations_other_app
    • nav_graph_activity_destinations_data_pattern.xml: android_views_navigation_activity_destinations_data_pattern
    • navigation/activitydestinations/ActivityDestinations.kt: android_views_navigation_activity_destinations_start_activity and android_views_navigation_activity_destinations_navigate_bundle
  • Dialog-destinations page (:views):
    • nav_graph_dialog_destinations.xml: android_views_navigation_dialog_destinations_dialog
  • Actions page (:views):
    • nav_graph_actions.xml: android_views_navigation_actions_define
    • navigation/actions/Actions.kt: android_views_navigation_actions_navigate
    • nav_graph_actions_global.xml: android_views_navigation_actions_global
  • Global-action page (:views):
    • nav_graph_global_action.xml: android_views_navigation_global_action_create
  • Navcontroller page:
    • :views, navigation/navcontroller/NavControllerCreate.kt: android_views_navigation_navcontroller_navhostfragment
    • :compose:snippets, navigation/navcontroller/NavControllerCreate.kt: android_compose_navigation_navcontroller_create

Dependencies, manifest, and supporting code

  • views/build.gradle.kts and gradle/libs.versions.toml: adds androidx.navigation:navigation-fragment-ktx, using the androidx-navigation version that's already in the catalog. :views had no Navigation dependency before. It provides NavHostFragment (Navcontroller page), NavController (Activity-destinations and Actions pages), the app: attributes that the navigation graphs use (startDestination, destination, popUpTo, popUpToInclusive, action, data, dataPattern, targetPackage, argType), and the navigators behind the graphs' <activity>, <fragment>, and <dialog> destinations. No other dependency is added.
  • views/src/main/AndroidManifest.xml: registers DestinationActivity, the activity that the Activity-destinations page's startActivity(Intent(context, DestinationActivity::class.java)) call launches. The class is at the bottom of ActivityDestinations.kt. It can't be private, because the system instantiates it.
  • The Kotlin regions sit in small private functions and classes that declare what the page's code assumes: navController, context, and an activity for startActivity() and supportFragmentManager. None of that is inside a region.

Compile-driven resources (required for the snippets to compile, no region tags):

  • views/src/main/res/values/activity_destinations.xml: the strings sampleActivityTitle and localActivityTitle, and the id simpleFragment. The Activity-destinations page's four graphs reference @string/sampleActivityTitle, @string/localActivityTitle, and app:startDestination="@id/simpleFragment", and resource linking fails while they're undefined. The page's graphs don't include a simpleFragment destination, so the id is declared on its own. The names come from the page.
  • views/src/main/res/values/navcontroller.xml: the id nav_host_fragment, for R.id.nav_host_fragment in the Navcontroller page's Kotlin. The name comes from the page.
  • nav_graph_actions.xml: the Actions page prints only the <fragment> element, so the file adds the <navigation> root around the region and a fragmentB destination after it. The action's app:destination="@id/fragmentB" has to resolve.
  • nav_graph_global_action.xml: the page's ... line is a plain exclude that hides the mainFragment destination, which app:startDestination and the global action point to. It renders as ..., like the page.

List of modifications (visible diffs vs. the page)

  • Activity-destinations, Dialog-destinations, Actions (global actions graph), and Global-action pages: the page's graphs start with <?xml version="1.0" encoding="utf-8"?>. That line has to be the first line of the file, ahead of the license header, so it can't be inside a region. These 7 regions start at <navigation.
  • Activity-destinations page, android_views_navigation_activity_destinations_navigate_bundle: bundleOf("userId" to "someUser") is now Bundle().apply { putString("userId", "someUser") }, on the same line. bundleOf is deprecated in the core-ktx version this repository uses ("This method does not provide type safety at compile time. Use the platform Bundle class directly instead"), so the original line builds with a deprecation warning. The guide's text already says the value is passed "with a Bundle".
  • Actions page, android_views_navigation_actions_global: adds xmlns:tools="http://schemas.android.com/tools" to the <navigation> element. The page's graph uses tools:layout without declaring the tools namespace, and aapt2 rejects it ("unbound prefix").
  • Actions page, android_views_navigation_actions_global: the comment Action back to destination which launched into this in_game_nav_graph now ends with a full stop.
  • Dialog-destinations page, android_views_navigation_dialog_destinations_dialog: the page's two standalone ... lines are <!-- ... --> comments. Nothing needs to be hidden there for the graph to compile.
  • Everything else is copied verbatim, including the page's class names in android:name (com.example.android.navigation.activity.DestinationActivity, com.example.FragmentA, com.example.android.gamemodule.*, androidx.navigation.myapp.MyDialogFragment) and the page's indentation. spotlessApply made no changes.

Lint reports InvalidNavigation warnings on 5 of the new graphs. These are warnings, not errors, and they come from the guide's code, which is kept as printed. The four Activity-destinations graphs set app:startDestination="@id/simpleFragment" without containing that destination, and the Dialog-destinations graph has no start destination, because the guide shows only the part of each graph it explains.

Snippets not migrated

  • Activity-destinations page: the AndroidManifest.xml block stays on the guide page, because manifest blocks are kept there.
  • Dialog-destinations page: the two Kotlin blocks (the Compose dialog() example and the Kotlin DSL graph) are left as they are on the guide page for now.
  • Global-action page: the "Use a global action" Kotlin and Java blocks are left as they are on the guide page for now.
  • The Java versions of the migrated Kotlin blocks (two on the Activity-destinations page, one on the Navcontroller page) aren't migrated. The guide pages are being updated separately to show the Kotlin versions.

Heads-up, unrelated to this PR and left untouched: spotlessApply logs "Skipping 'build.gradle.kts' because it does not converge" for views/build.gradle.kts, which has a nonstandard license header on main.

Build: ./gradlew :views:spotlessApply :compose:snippets:spotlessApply (no changes), then ./gradlew :views:build and ./gradlew :compose:snippets:build pass.

@snippet-bot

snippet-bot Bot commented Oct 23, 2026

Copy link
Copy Markdown

Here is the summary of changes.

You are about to add 13 region tags.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant