Jean Jimenez Jean Jimenez - 1 year ago 71
Android Question

How can I make my custom objects Parcelable?

I'm trying to make my objects Parcelable. However, I have custom objects and those objects have

attributes of other custom objects I have made.

What would be the best way to do this?

Answer Source

You can find some examples of this here, here (code is taken here), and here.

You can create a POJO class for this, but you need to add some extra code to make it Parcelable. Have a look at the implementation.

public class Student implements Parcelable{
        private String id;
        private String name;
        private String grade;

        // Constructor
        public Student(String id, String name, String grade){
   = id;
   = name;
            this.grade = grade;
       // Getter and setter methods

       // Parcelling part
       public Student(Parcel in){
           String[] data = new String[3];

  = data[0];
  = data[1];
           this.grade = data[2];

       public int describeContents(){
           return 0;

       public void writeToParcel(Parcel dest, int flags) {
           dest.writeStringArray(new String[] {,
       public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
           public Student createFromParcel(Parcel in) {
               return new Student(in); 

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

Once you have created this class, you can easily pass objects of this class through the Intent like this, and recover this object in the target activity.

intent.putExtra("student", new Student("1","Mike","6"));

Here, the student is the key which you would require to unparcel the data from the bundle.

Bundle data = getIntent().getExtras();
Student student = (Student) data.getParcelable("student");

This example shows only String types. But, you can parcel any kind of data you want. Try it out.

EDIT: Another example, suggested by Rukmal Dias.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download