'How to pass a List as an argument to a Fragment using Navigation Component safeargs
The documentation discusses how to send simple integers and strings. For example:
<argument
android:name="myIntArg"
android:defaultValue="255"
app:argType="integer" />
In the origin Fragment:
val action = OriginFragmentDirections.myAction(myInt)
findNavController().navigate(action)
In the destination Fragment:
val receivedInt = DestinationFragmentArgs.fromBundle(arguments).myIntArg
But say instead of myIntArg, I wanted to send a list of integers (myIntListArg). How would I do that? What app:argType would I use in my argument?
Solution 1:[1]
You can add array arguments like this:
<argument
android:name="variableName"
app:argType="className[]"/>
Note that "className" must be set like "com.google.package.ClassName".
Solution 2:[2]
This is how you can pass list of integers.
<argument
android:name="yourVariableName"
app:argType="integer[]"
app:nullable="false"/>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Cristan |
| Solution 2 | Bhavya Sikka |
