But what if we want to pass data for the next fragment. In this chapter we are going to learn about passing data between fragments.
GitHub
If you haven’t done the previous chapters, then from GitHub you can download the starter project for this chapter.
GitHub – add_library_and_make_action branch
Step 1 – Arguments
Navigation Component allows us to attach data to a navigation operation by defining arguments for a destination. In our example we are going to pass a string, the content of an EditText.
Add argument
To pass data between fragments, first we have to define the argument in the navigation graph. The argument has to be added to the destination that receives it.
- In the popup window name the attribute as passedData… very creative. 😎
- The Type will be String.
- Leave unchecked the Array and the Nullable boxes
- and set the Default value to “No passed data”
- Click on the OK button.
If you now open the text view of the navigation_graph.xml file, then inside of the Fragment_2‘s part, you should see the argument.
the arugment of passedData
Finally in this step rebuild the app. From the top menu select the Build option, then at the bottom click on the Rebuild project. Due to the rebuild, the generated source will include the now created argument. It means, we can refer to it in the next step.
Step 2 – Pass the argument
Now open the Fragment_1::class. In the previous chapter we defined the click listener for the btn_fragment_1 button and we have created an instance about the action in the second line. Currently the actionFragment1ToFragment2() method doesn’t have any arguments. Because of the rebuild, we can pass here the text of the et_fragment_1 EditText. So just extend the line like this:
Pass the argument
Step 3 – Get the argument
Finally open the Fragment_2::class as well. In the very last chapter we defined here also the click listener for the button inside of the onViewCreate() method.
First option
After the rebuild we can use one more autogenerated object. The name of it cames from the name of the fragment, which is extended by the word Args. So the name will be in our case: Fragment_2Args.
We will use this class’s fromBundle() method to retrieve the arguments. Calling this method, we can reach the argument, what we have craeted. So the code looks like this:
Get the argument
Second option
When using the -ktx dependencies, Kotlin users can also use the by navArgs() property delegate to access arguments. For this we have to define a member variable, called args.
private val args : Fragment_2Args by navArgs()
Then inside of the onViewCreate() method we can call on the args variable the passedData property.
tv_passed_data.text = args.passedData
It depends on you which one do you prefer. The result is going to be the same.
Run the app
Finally run the app and try it out. Write something in the EditText of the Fragment_1 and click on the button. In the Fragment_2 you should see the string.
Before we finishing this chapter, let’s try out something. In the Fragment_1 remove the argument form the actionFragment1ToFragment2() mehtod and run again the app. No matter what you type in the EditText, in the destination the value of the text will be the default value of the argument, which is “No passed data”.
GitHub
The source code is available on GitHub
Questions
I hope the description was understandable and clear. But if you have still questions, then leave me comments below! 😉
Have a nice a day! 🙂