12

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?

JHowzer
  • 3,684
  • 4
  • 30
  • 36
  • 1
    Support to list of objects coming in alpha8: https://issuetracker.google.com/issues/111487504 – Alex Dec 02 '18 at 06:39
  • What are we actually supposed to do right now to send a list of strings to another fragment and use this component :/ – Daniel Wilson Dec 05 '18 at 15:03
  • I wouldn't pass a list of objects to a fragment via intent though; Intent sizes are limited in size (1mb?, can't remember now). I would pass an ID or something that can allow the fragment to retrieve the corresponding data. and if you TRULY want to pass this _now_ without refactoring, you could transform your list of integers to a comma separated string and then decode on the other side (why though?), then you just pass a "string" via intent. I would not do this, but it would work fine as long as the string is smaller than the max intent size. – Martin Marconcini Jun 06 '19 at 12:38
  • Disclaimer for the future readers: If your list is guaranteed to be always "short", then - when they add support to this - I wouldn't consider it bad if you pass a small number of integers in an array, but you'd have to make sure you don't abuse the mechanism in a way it's not intended, that's what I'm trying to say here. :) – Martin Marconcini Jun 06 '19 at 12:42

2 Answers2

16

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".

Cristan
  • 12,083
  • 7
  • 65
  • 69
-1

This is how you can pass list of integers.

    <argument
        android:name="yourVariableName"
        app:argType="integer[]"
        app:nullable="false"/>