Jetpack Navigation for fragments: nav graphs, Safe Args, and deep links

The Jetpack Navigation component (androidx.navigation) handles moving between screens in an Android app. You describe your screens and the paths between them in a navigation graph, and a NavController performs the fragment transactions, manages the back stack, wires up the Up button, and resolves deep links for you.
This post covers the XML-graph, fragment-based setup with Navigation 2.10.2. If you’re building with Jetpack Compose, use navigation-compose with type-safe @Serializable routes (stable since 2.8.0), or look at Navigation 3, a separate Compose-only library (androidx.navigation3).
The pieces
Section titled “The pieces”- Navigation graph: an XML resource in
res/navigation/listing destinations and the actions between them. - Destination: a screen you can navigate to, usually a fragment.
- Action: a named, reusable path from one destination to another.
- NavHost: the container that swaps destinations in and out. For fragments this is a
NavHostFragmentinside aFragmentContainerView. - NavController: the object you call
navigate()on. EachNavHosthas one.
Add the dependencies
Section titled “Add the dependencies”In the module’s build.gradle.kts:
dependencies { val navVersion = "2.10.2" implementation("androidx.navigation:navigation-fragment:$navVersion") implementation("androidx.navigation:navigation-ui:$navVersion")}Older guides use navigation-fragment-ktx and navigation-ui-ktx. Those artifacts still get published, but they’re empty: the Kotlin extensions (findNavController(), navArgs(), and so on) now live in the main artifacts.
Create the graph
Section titled “Create the graph”Create res/navigation/nav_graph.xml (in Android Studio: New > Android Resource File, resource type Navigation). A minimal graph with two fragments and one action:
<?xml version="1.0" encoding="utf-8"?><navigation xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/nav_graph" app:startDestination="@id/firstFragment">
<fragment android:id="@+id/firstFragment" android:name="com.example.app.FirstFragment" tools:layout="@layout/fragment_first"> <action android:id="@+id/action_firstFragment_to_secondFragment" app:destination="@id/secondFragment" /> </fragment>
<fragment android:id="@+id/secondFragment" android:name="com.example.app.SecondFragment" tools:layout="@layout/fragment_second" /></navigation>Add the NavHost
Section titled “Add the NavHost”Put a FragmentContainerView hosting a NavHostFragment in the activity layout:
<androidx.fragment.app.FragmentContainerView android:id="@+id/nav_host_fragment" android:name="androidx.navigation.fragment.NavHostFragment" android:layout_width="match_parent" android:layout_height="match_parent" app:defaultNavHost="true" app:navGraph="@navigation/nav_graph" />app:defaultNavHost="true" makes this host intercept the system Back button.
Navigate
Section titled “Navigate”From inside a fragment, navigate by action ID:
import androidx.navigation.fragment.findNavController
binding.nextButton.setOnClickListener { findNavController().navigate(R.id.action_firstFragment_to_secondFragment)}Pass arguments with Safe Args
Section titled “Pass arguments with Safe Args”Safe Args is a Gradle plugin that generates a Directions class per source destination and an Args class per destination with arguments, so argument names and types are checked at compile time.
-
Add the plugin to the top-level
build.gradle.kts:buildscript {repositories {google()}dependencies {classpath("androidx.navigation:navigation-safe-args-gradle-plugin:2.10.2")}} -
Apply it in the app module. Use
androidx.navigation.safeargs.kotlinfor Kotlin-only modules andandroidx.navigation.safeargsfor Java or mixed modules:plugins {id("androidx.navigation.safeargs.kotlin")} -
Declare the argument on the destination:
<fragmentandroid:id="@+id/secondFragment"android:name="com.example.app.SecondFragment"tools:layout="@layout/fragment_second"><argumentandroid:name="exampleArg"app:argType="string" /></fragment> -
Navigate with the generated
Directionsclass. BecauseexampleArghas no default, it becomes a required parameter:val action = FirstFragmentDirections.actionFirstFragmentToSecondFragment(exampleArg = "Hello, World!")findNavController().navigate(action) -
Read it in the destination with the
navArgs()delegate:import androidx.navigation.fragment.navArgsclass SecondFragment : Fragment(R.layout.fragment_second) {private val args: SecondFragmentArgs by navArgs()override fun onViewCreated(view: View, savedInstanceState: Bundle?) {super.onViewCreated(view, savedInstanceState)val exampleArg = args.exampleArg}}
Deep links
Section titled “Deep links”An implicit deep link lets a URI open a destination directly. Add a <deepLink> to the destination; placeholders in {braces} are matched to the destination’s declared arguments:
<fragment android:id="@+id/secondFragment" android:name="com.example.app.SecondFragment" tools:layout="@layout/fragment_second"> <argument android:name="exampleArg" app:argType="string" /> <deepLink app:uri="example://second?exampleArg={exampleArg}" /></fragment>Then point the activity that hosts the graph at it in AndroidManifest.xml. At build time, Navigation replaces <nav-graph> with the matching <intent-filter> elements:
<activity android:name=".MainActivity" android:exported="true"> <nav-graph android:value="@navigation/nav_graph" /></activity>Leave the activity on the default standard launch mode. With singleTop, Navigation doesn’t handle the deep link automatically when the activity is reused, so you’d have to call navController.handleDeepLink(intent) yourself in onNewIntent().
For an explicit deep link, for example from a notification, build a PendingIntent with NavDeepLinkBuilder:
val pendingIntent = NavDeepLinkBuilder(context) .setGraph(R.navigation.nav_graph) .setDestination(R.id.secondFragment) .setArguments(bundleOf("exampleArg" to "from-notification")) .createPendingIntent()Gotchas
Section titled “Gotchas”- Calling
navigate()twice quickly (a double tap) can throwIllegalArgumentExceptionbecause the second call runs from a destination that no longer has that action. Debounce the click, or checkfindNavController().currentDestination?.idfirst. - Safe Args only generates code for XML graphs. If you build the graph in Kotlin (the Kotlin DSL or Compose), use
@Serializableroute classes instead. - To connect a
Toolbar,BottomNavigationView, orNavigationViewto the graph, use the helpers innavigation-ui(setupWithNavController).
Further reading
Section titled “Further reading”© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)