Profile
December 27, 2022

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:
 

Typeapp:argType syntaxSupport for default valuesHandled by routesNullable
Integerapp:argType="integer"YesYesNo
Floatapp:argType="float"YesYesNo
Longapp:argType="long"Yes - Default values must always end with an 'L' suffix (e.g. "123L").YesNo
Booleanapp:argType="boolean"Yes - "true" or "false"YesNo
Stringapp:argType="string"YesYesYes
Resource Referenceapp:argType="reference"Yes - Default values must be in the form of "@resourceType/resourceName" (e.g. "@style/myCustomStyle") or "0"YesNo
Custom Parcelableapp:argType="<type>", where <type> is the fully-qualified class name of the ParcelableSupports a default value of "@null". Does not support other default values.NoYes
Custom Serializableapp:argType="<type>", where <type> is the fully-qualified class name of the SerializableSupports a default value of "@null". Does not support other default values.NoYes
Custom Enumapp:argType="<type>", where <type> is the fully-qualified name of the enumYes - Default values must match the unqualified name (e.g. "SUCCESS" to match MyEnum.SUCCESS).NoNo

 

Github Code Link

Last edited on December 27, 2022 by Admin

Latest articles