Make a custom java object Parcelable in Android

Raunak picture Raunak · Sep 26, 2011 · Viewed 8.2k times · Source

I am trying to send an object of type MyCustomObject to another activity via an intent. I know, to make the class Parcelable, I should do public class MyCustomObject implements Parcelable, but I am not sure how custom object arrays work in parcelable. Here's what I have got so far.. Also, do I need to make the Search class implements Parcelable too?

Here is my updated answer I now get a null object when I do intent.getParcelableExtra(search). Maybe I am not creating the search array properly?

public class MyCustomObject implements Parcelable{
    public Search search[];

    public MyCustomObject(Parcel in){
        in.readArray(MyCustomObject.class.getClassLoader());
    }

    @Override
    public int describeContents(){
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags){
        dest.writeArray(search)
    }

    public static final Parcelable.Creator<MyCustomObject> CREATOR = new Parcelable.Creator<MyCustomObject>(){
        @Override
        public MyCustomObject createFromParcel(Parcel source){
            return new MyCustomObject(source);
        }

        @Override
        public MyCustomObject[] newArray(int size){
            return new MyCustomObject[size];
        }
    }

    public static class Search implements Parcelable{
        public int rank;
        public String title;
        public String[] imageURL;

        public Search(Parcel in){
            rank = in.readInt();
            title = in.readString();

            String[] url = new String[4];
            in.readStringArray(url);
            url = imageURL
        }

        @Override
        public int describeContents(){
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags){
            dest.writeInt(rank);
            dest.writeString(title);
            dest.writeStringArray(imageURL);
        }

        public static final Parcelable.Creator<Search> CREATOR = new Parcelable.Creator<Search>(){

        @Override
        public Search createFromParcel(Parcel source){
            return new Search(source);
        }

        @Override
        public Search[] newArray(int size){
            return new Search[size];
        }
    }

    }
}

Answer

michaelg picture michaelg · Sep 26, 2011

A couple of things. First, your Search object will need to implement Parcelable as well. Next, you need to create a static field called CREATOR that implements Parcelable.Creator<MyCustomObject>. This will have a method called createFromParcel that returns an instance of MyCustomObject. That is where you read data out of a parcel and create a new instance of MyCustomObject. This is essentially the opposite of writeToParcel. This all applies to Search as well since you must make it implement Parcelable. In summary:

  • Make Search implement Parcelable
  • Implement Search.writeToParcel
  • Create a static field in Search called CREATOR that implements Parcelable.Creator<Search>
  • Implement the createFromParcel method of Search.CREATOR
  • Create a static field in MyCustomObject called CREATOR that implements Parcelable.Creator<MyCustomObject>
  • Implement the createFromParcel method of MyCustomObject.CREATOR