It may happen that your class will have child classes, so each of them in this case can return different values ββto describeContent() , so you need to know what specific type of object should be created from Parcel . For example, for example, an example implementation of Parcelable methods in a parent class ( MyParent ):
//************************************************ // Parcelable methods //************************************************ //need to be overwritten in child classes //MyChild_1 - return 1 and MyChild_2 - return 2 public int describeContents() {return 0;} public void writeToParcel(Parcel out, int flags) { out.writeInt(this.describeContents()); out.writeSerializable(this); } public Parcelable.Creator<MyParent> CREATOR = new Parcelable.Creator<MyParent>() { public MyParent createFromParcel(Parcel in) { int description=in.readInt(); Serializable s=in.readSerializable(); switch(description) { case 1: return (MyChild_1 )s; case 2: return (MyChild_2 )s; default: return (MyParent )s; } } public MyParent[] newArray(int size) { return new MyParent[size]; } };
In this case, you do not need to implement all Parcelable methods in child classes - except describeContent()
barmaley Jan 24 '11 at 5:50 a.m. 2011-01-24 05:50
source share