Passing Data to a Destination Fragment in Android Using Safe Args, Part 2

Anil Kr Mourya
2 min readJul 11, 2023

--

As you can see in Part 1 of my blog, Implementation Of Fragment Navigation Using Navigation Components, Now we are going to implement passing data between two fragments using Navigation components with Safe arguments.

Follow these steps:

1 : Set up SafeArg : Safe Args is a Gradle plugin that automatically generates classes for type-safe navigation and data passing between destinations.

a. Add the Safe Args plugin In your project-level build.gradle file.

buildscript {
repositories {
mavenCentral()
google()
}
dependencies {
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.6.0"
}
}

b. In your app-level build.gradle file, apply the Safe Args plugin, and include the navigation-safe-args plugin.



plugins {

id 'androidx.navigation.safeargs'

}

2 :Define the argument in the destination fragment: In the destination fragment where you want to receive the data, define an argument using the argument directive in the fragment's navigation graph XML file (mobile_navigation.xml).

<fragment
android:id="@+id/addNoteFragment"
android:name="com.nic.roomdatabase.Fragment.AddNoteFragment"
android:label="Add Note"
tools:layout="@layout/fragment_add_note" >
<action
android:id="@+id/action_addNoteFragment_to_home_fragment"
app:destination="@id/home_fragment" />
<argument

android:name="TaskItem"
app:argType="string"
app:nullable="true"
android:defaultValue="@null" />
</fragment>

we can define an argument name “TaskItem” with the argtype “string or any model class or data class ” you can define argtype as per your requriment, now I am passing argtype is String for the destination fragment.

3 : Navigate to the destination fragment with data: In the source fragment, when you want to navigate to the destination fragment, use the findNavController().navigate() method and pass the appropriate action ID from your navigation graph. You can use the generated Safe Args class to pass the data.

val data = "Hello, Destination Fragment!"

val action = ListofNoteFragmentDirections.actionMobileNavigationToAddNoteFragment().setTaskItem(data)

findNavController(it).navigate(action)

we create a String variable called "data" with the value we want to pass to the destination fragment. We use the actionMobileNavigationToAddNoteFragment() method, which is generated by Safe Args based on the action ID in the navigation graph, to create an instance of the action. We then pass the "data" variable to the action and navigate to the destination fragment using findNavController().navigate().

4 : Retrieve the data in the destination fragment: In the destination fragment, retrieve the passed data using the generated Safe Args class.

arguments?.let {
val data = AddNoteFragmentArgs.fromBundle(it).taskItem
}

we use the AddNoteFragmentArgs class, generated by Safe Args, to access the data passed to the destination fragment. The args.data property gives us access to the passed data.

By following these steps and utilizing Safe Args, you can pass data to a destination fragment and navigate between fragments in Android. Safe Args simplifies the process by generating the necessary classes for type-safe data passing, reducing the risk of errors and improving overall code quality.

Project on GitHub

If any Query please comment…………… thank you

--

--

Anil Kr Mourya
Anil Kr Mourya

Written by Anil Kr Mourya

Unlocking the Power of Developers : Your Imagination, Our Innovation. LinkedIn Profile https://www.linkedin.com/in/anil-kr-maurya-351a48283/

No responses yet