The recommended way to navigate between destinations is to use the Safe Args Gradle plugin. This plugin generates simple object and builder classes that enable type-safe navigation between destinations. Safe Args is recommended both for navigating as well as passing data between destinations.
Enable Safe Args
To add Safe Args
to your project, include the following classpath
in your top level build.gradle
file:
Groovy
buildscript { repositories { google() } dependencies { def nav_version = "2.7.5" classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version" } }
Kotlin
buildscript { repositories { google() } dependencies { val nav_version = "2.7.5" classpath("androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version") } }
You must also apply one of two available plugins.
To generate Java language code suitable for Java or mixed Java and Kotlin modules, add
this line to your app or module's build.gradle
file:
Groovy
plugins { id 'androidx.navigation.safeargs' }
Kotlin
plugins { id("androidx.navigation.safeargs") }
Alternatively, to generate Kotlin code suitable for Kotlin-only modules add:
Groovy
plugins { id 'androidx.navigation.safeargs.kotlin' }
Kotlin
plugins { id("androidx.navigation.safeargs.kotlin") }
You must have android.useAndroidX=true
in your
gradle.properties
file as per
Migrating to AndroidX.
Generated code
After enabling Safe Args, your generated code contains classes and methods for each action you've defined as well as classes that correspond to each sending and receiving destination.
Safe Args generates a class for each destination where an action originates. The
generated class name adds "Directions" to the originating destination class
name. For example, if the originating destination is named
SpecifyAmountFragment
, the generated class is named
SpecifyAmountFragmentDirections
.
The generated class contains a static method for each action defined in the
originating destination. This method takes any defined action parameters
as arguments and returns a NavDirections
object that you can pass
directly to navigate()
.
Safe Args example
For example, consider a navigation graph with a single action that connects two
destinations, SpecifyAmountFragment
and ConfirmationFragment
. The
ConfirmationFragment
takes a single float
parameter that you provide as part
of the action.
Safe Args generates a SpecifyAmountFragmentDirections
class with a single
method, actionSpecifyAmountFragmentToConfirmationFragment()
, and an inner
class called ActionSpecifyAmountFragmentToConfirmationFragment
. The inner
class is derived from NavDirections
and stores the associated action ID and
float
parameter. The returned NavDirections
object can then be passed
directly to navigate()
, as shown in the following example:
Kotlin
override fun onClick(v: View) {
val amount: Float = ...
val action =
SpecifyAmountFragmentDirections
.actionSpecifyAmountFragmentToConfirmationFragment(amount)
v.findNavController().navigate(action)
}
Java
@Override
public void onClick(View view) {
float amount = ...;
action =
SpecifyAmountFragmentDirections
.actionSpecifyAmountFragmentToConfirmationFragment(amount);
Navigation.findNavController(view).navigate(action);
}
For more information about passing data between destinations with Safe Args, see Use Safe Args to pass data with type safety.