How to pass data using SafeArgs in Android Navigation
In Android programming, navargs is a term that refers to arguments that are passed to a navigation destination when navigating using the Android Navigation component.
The Android Navigation component is a part of the Android Jetpack suite of libraries that provides a powerful and flexible way to manage navigation in an Android app. It helps you design a consistent navigation structure and implement navigation in a way that is easy to maintain and test.
The navargs
class is generated automatically by the Android Navigation component based on the arguments that are defined in the navigation graph. It contains fields for each of the arguments that are passed to the destination, as well as getter and setter methods for those arguments.
To use SafeArgs in the Android navigation component, you will need to follow these steps:
1. Add the SafeArgs plugin to your project-level build.gradle
file:
buildscript {
dependencies
{ // Add this line
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
}
}
OR on Latest Android Project:
plugins {
// Add this line
id 'androidx.navigation.safeargs.kotlin' version '2.5.1' apply false
}
2. Apply the SafeArgs plugin to your app-level build.gradle
file:
apply plugin: "androidx.navigation.safeargs"
OR on Latest Android Project:
plugins {
// Add this line
id 'androidx.navigation.safeargs.kotlin'
}
3. Define the arguments for your destination in the navigation graph. You can do this either by using the Navigation Editor in Android Studio or by manually adding them to the navigation XML file. For example:
<fragment
android:id="@+id/SecondFragment"
android:name="com.Abdulqa.androidnavigationsafeargsexample.SecondFragment"
android:label="@string/second_fragment_label"
tools:layout="@layout/fragment_second">
<argument
android:name="stringData" // it can be any name
app:argType="string" /> // type of argument list shared below
</fragment>
4. Build your project to generate the SafeArgs classes. These classes will be located in the app/build/generated/source/navigation/
directory and will have the same package as your app.
5. Use the generated SafeArgs classes to pass data between destinations. For example:
val action = FirstFragmentDirections.actionFirstFragmentToSecondFragment("Hello from SafeArgs!")
findNavController().navigate(action)
6. In the destination fragment, you can retrieve the argument value using the generated SafeArgs class:
private val args: SecondFragmentArgs by navArgs()
binding.textviewSecond.text = args.stringData
That's it! You have successfully used SafeArgs in your Android navigation component to pass data between destinations.
Android SafeArgs Navigation supported Argument Types:
Type | app:argType syntax | Support for default values | Handled by routes | Nullable |
---|---|---|---|---|
Integer | app:argType="integer" | Yes | Yes | No |
Float | app:argType="float" | Yes | Yes | No |
Long | app:argType="long" | Yes - Default values must always end with an 'L' suffix (e.g. "123L"). | Yes | No |
Boolean | app:argType="boolean" | Yes - "true" or "false" | Yes | No |
String | app:argType="string" | Yes | Yes | Yes |
Resource Reference | app:argType="reference" | Yes - Default values must be in the form of "@resourceType/resourceName" (e.g. "@style/myCustomStyle") or "0" | Yes | No |
Custom Parcelable | app:argType="<type>", where <type> is the fully-qualified class name of the Parcelable | Supports a default value of "@null". Does not support other default values. | No | Yes |
Custom Serializable | app:argType="<type>", where <type> is the fully-qualified class name of the Serializable | Supports a default value of "@null". Does not support other default values. | No | Yes |
Custom Enum | app:argType="<type>", where <type> is the fully-qualified name of the enum | Yes - Default values must match the unqualified name (e.g. "SUCCESS" to match MyEnum.SUCCESS). | No | No |