Android: Passing ArrayList of Object Within an Intent

An intent is an object that is very useful to pass variables between activities in Android. Basic primitive data types such as Integer, String, Boolean are easy to be passed through an intent.

On my previously written article on how to switch between android activities I already explained on how to pass a char sequence variable. But Android has no custom object data type that can be passed directly through an intent as in primitive data types. So how do we able to pass such kind of custom object data?

The answers is: “flatten the object into a Parcelable object, passing it into the intent, and rebuild the flatten object into the original object”. More information on parcelable can be seen here.

Better understanding on how to do this technically is by an example. Lets say I have a list of array (an ArrayList) of a Car class object which is going to be passed through an intent. Below is the definition of the Car class:

class Car {
    public int regId;
    public String brand;
    public String color;
}

in order the Car class become parcelable, it has to implement Parcelable class and it’s required method. So the Car class has to be modified as below:

class Car implements Parcelable {
    public int regId;
    public String brand;
    public String color;

    public int describeContents() {
	return this.hashCode();
    }

    public void writeToParcel(Parcel dest, int flags) {
	dest.writeInt(regId);
	dest.writeString(brand);
	dest.writeString(color);
    }

}

OK, we’re done for this one. As for another requirement for Parcelable class, the class which implements the Parcelable class has to have an object of Parcelable.Creator<T> class type named CREATOR. So let’s add it into the Car class like below:

class Car implements Parcelable {
    public int regId;
    public String brand;
    public String color;

    public Car(Parcel source) {
        regId = source.getInt();
        brand = source.getString();
        color = source.getString();
    }

    public Car(int regId, String brand, String color) { 
        this.regId = regId;
        this.brand = brand;
        this.color = color;
    }

    public int describeContents() {
	return this.hashCode();
    }

    public void writeToParcel(Parcel dest, int flags) {
	dest.writeInt(regId);
	dest.writeString(brand);
	dest.writeString(color);
    }

    public static final Parcelable.Creator CREATOR
             = new Parcelable.Creator() {
         public Car createFromParcel(Parcel in) {
             return new Car(in);
         }

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

}

A Parcelable.Creator class requires two method, createFromParcel(Parcel) and newArray(int). the createFromParcel(Parcel) method returning Car object from Parcel. So we have to add a constructor with a Parcel as it’s parameter. The order of get[dataType]() function has to be in the same order as in writeToParcel() method. As an addition I also added another constructor for convenience in creating Car object.

We’re done in creating a parcelable Car class. So now we can pass the Car class into an intent like below:

ArrayList carList = new ArrayList();
carList.add(new Car('1','Honda','Black'); 
carList.add(new Car('2','Toyota','Blue'); 
carList.add(new Car('3','Suzuki','Green'); 
Intent i = new Intent(getApplicationContext(), CarDetailActivity.class);
i.putParcelableArrayListExtra("cars", carList);
this.startActivity(i);

In CarDetailActivity class getting those extra are as follow:

Intent i = this.getIntent();
ArrayList<Car> carList = (ArrayList<Car>) i.getParcelableArrayListExtra("cars");

// then, do what you want with the ArrayList.

Hope this tutorial is useful to you and found that Parcelable class is useful to flatten a custom Object and pass it through an intent.