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.

79 Replies to “Android: Passing ArrayList of Object Within an Intent”

  1. DownloadMp3-Gratis adalah website Download lagu Gratis, free download mp3 Indonesia, Soundcloud – 4shared mp3 download, mp3 download musik online berkualitas tinggi, situs update chart musik tercepat akurat, planet lagu, gudang musik, gudang lagu paling besar bisa memenuhi semua kebutuhan pengguna, menjadi pilihan pertama untuk anda.

  2. CAFELAGU berkembang menjadi situs yang tak hanya berbagi info, tapi juga tempat untuk berekspresi, review unduhan musik, dan ribuan lirik lagu bagi pecinta musik lainnya.

    Selamat datang di CAFELAGU! Semoga anda puas dan menikmati konten-konten yang kami sediakan. Silakan kirimkan saran,

  3. Adiak urang tapandang
    Tingga di rumah gadang
    Rang kayo gadang
    Samanjak adiak den pinang
    Kini tingga kanangan
    Tabaok seso sansaro badan
    Samanjak adiak den pinang
    Kini tingga kanangan
    Tabaok seso sansaro badan

  4. Neon baeu ppyam ttaeryeo
    Hyeonggwangdeung arae isseul ttaedo
    Neon gasu ppyam ttaeryeo
    Papsongeul heungeolgeoril ttaedo
    Geuttaedo
    Yeogin neomu sikkeureowo
    Jogeumman deo gakkai wa
    Come close
    Neon neomu yeppeo oraenmanui tteollimiya

  5. Gudang lagu mp3 terbaru, download lagu mp3 terbaik gratis, dengarkan lagu mp3 secara online, top download lagu Indonesia. download lagu mp3 gratis, download lagu mp3 terbaru 2019, Stafa Band Tangga Lagu – Download lagu terbaru 2019 Indonesia planetlagu. Gratis Lagu Barat Dangdut Koplo Full Album Terlengkap wapka mobi, download lagu barat hits terbaru 2019.

  6. website Download lagu Gratis, free download mp3 Indonesia, download musik online berkualitas tinggi, gudang lagu paling besar bisa memenuhi semua kebutuhan pengguna, menjadi pilihan pertama untuk anda.

  7. I’m sorry but
    Don’t wanna talk
    I need a moment before I go
    It’s nothing personal
    I draw the blinds
    They don’t need to see me cry
    ‘Cause even when they understand
    They don’t understand
    So then when I’m finished
    I’m all ’bout my business
    And ready to save the world
    I’m taking my misery
    Make it my bitch
    Can’t be everyone’s favorite girl

  8. Bila kamu mengunduh lagu Alan Walker – Alone MP3 usahakan hanya untuk review saja, jika memang kamu suka dengan lagu Alan Walker – Alone belilah kaset asli yang resmi atau CD official lagu tersebut, kamu juga bisa mendownload secara legal di Official iTunes Alan Walker – Alone, untuk mendukung Alan Walker – Alone di semua charts dan tangga lagu Indonesia.

  9. works around the clock to be the first to deliver minute to minute breaking news, gossip, and the most exclusive coverage on the hottest K-pop stars.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

CAPTCHA Image

*