Home » Android » android – Safe Args: use list of parcelables

android – Safe Args: use list of parcelables

Posted by: admin May 14, 2020 Leave a comment


I am using the Safe Args plugin with the new Navigation components for my Android project. Now I have an argument that is an array list of parcelables, is there a way to use this with the Safe Args Plugin?

Something like app:argType=ParcelableArray. This should be possible since there are bundle methods like putParcelableArrayList().

How to&Answers:

Currently i don’t think there is a simple way to use list of parcelables with safe args,
But i have found some “hack” to make this work.
For example, i have object ‘User’ and it parcelable, i am declaring a new parcelable object ‘Users’ that extending ArrayList().

data class User(var name: String, val age: Int): Parcelable

class Users: ArrayList<User>(), Parcelable

Now i can set ‘Users’ as argument in navigation


And passing array list of parcelables between destinations:

 val user=User("Alex", 36)
 val users= Users()
 val action=MainFragmentDirections.actionMainFragmentToSecondFragment(users)
 NavHostFragment.findNavController([email protected]).navigate(action)

And to retrieve them on other destination:

val users=SecondFragmentArgs.fromBundle(arguments).users
val user=users[0]


Support to list of objects coming in alpha8:


Yes, since version 1.0.0-alpha08 you can now pass arrays of parcelable objects like this:


For passing arrays of primitive types use for e.g. app:argType="integer[]"


An improvement to @LaVepe suggestion: as for Android Studio 3.4.2 you can pass Parcelable array with in-built feature of navigation editor by specifying Arguments for chosen destination. Just check ‘Array’ option after choosing your custom Parcelable class which should not be wrapped in any collection beforehand:


Here how it looks in xml:

        app:argType="com.company.domain.Item[]" />

In your Fragment/Activity code you might strictly pass a typed array of model Parcelable items. If you have non-array collection and write in Kotlin, it may be achieved with (if you have list beforehand) list.toTypedArray().