DEV Community


Pass list of objects in Intent using Kotlin

Law Gimenez
Senior Mobile Developer, I can develop both iOS and Android.
Originally published at ・1 min read

Every Android developer is familiar with passing data between Activities using Bundle. The old Java way was to implement Parcelable class and then you override all the required methods. See example below. You can read more of the details here.

public class MyParcelable implements Parcelable {
     private int mData;

     public int describeContents() {
         return 0;

     public void writeToParcel(Parcel out, int flags) {

     public static final Parcelable.Creator<MyParcelable> CREATOR
             = new Parcelable.Creator<MyParcelable>() {
         public MyParcelable createFromParcel(Parcel in) {
             return new MyParcelable(in);

         public MyParcelable[] newArray(int size) {
             return new MyParcelable[size];

     private MyParcelable(Parcel in) {
         mData = in.readInt();
Enter fullscreen mode Exit fullscreen mode

I’m not really a fan of this type of implementation because it will make my data class more difficult to read and confusing.

Typically it is complicated to pass a list of objects between Activities. But lately in Kotlin all you have to do is simple.

First, in your data class you implement Serializable.

data class ItemCount(
    @ColumnInfo(name = "id") val itemID: Int,
    @ColumnInfo(name = "name") val name: String
): Serializable {
    @PrimaryKey(autoGenerate = true) var id: Int? = null
Enter fullscreen mode Exit fullscreen mode

Then in your source Activity, you add it to your bundle and cast it as Serializable.

val intent = Intent(this,
intent.putExtra("list", listItemCount as Serializable)
Enter fullscreen mode Exit fullscreen mode

In your destination Activity, you retrieved the list.

val listItemCount: MutableList<ItemCount>  = intent.getSerializableExtra("list") as MutableList<ItemCount>
Enter fullscreen mode Exit fullscreen mode

And that’s it.

Discussion (2)

gouravkhunger profile image
Gourav Khunger

Amazing! I got to learn something new, thanks for the writeup 🙂

lawgimenez profile image
Law Gimenez Author

Thank you! Glad you liked it. I just discovered it so I wrote it right away so I won't forget.