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.
knowledge for me
New things for me. Thanks
thank you sir for information 🙂
yo wes ben
Nice Info, i will try this
Terima kasih pak Aryo. Saya sudah mencoba materi yang bapak berikan. Sangat membantu
thanks info nya
Thanks, nice
Nice artikel
nice article
Nice post
Nice post bro
ok bro thanks you nian nih bro
provides a free temporary email address that is secure and anonymous to keep your real mailbox clean and safe.
Ga paham kak
Hard to ignore such an amazing article like this. Keep on writing!
What your declaring is entirely genuine article. Thanks
Hello. This is an impressive post. Keep on blogging
Continue for sharing such a excellent post here.
You may have found a credit slot site without a discount at this time, but finding a trusted one requires more effort. We are a trusted and most popular credit deposit slot site in 2022. Playing credit deposit slots is now very easy to play anywhere and anytime
The fact that this is how me feel is amazing. Thank you
Hi!!! I love your blog! Great content. Please do visit my webpage and learn more about online casino and sportstoto. Here’s more interesting site:
Good article, thank you for sharing
wow, great article !
Good post,,
I hope this one affects more widely so that many people could be happier than before. I have something simmilar too. This could also make people feel much better I guess.
I am really grateful for your blog post for giving a lot of information about android