Description Purpose Contents () Parcelable interface

Possible duplicate:
Fixed when / when describesContents () is used?

What is the purpose of implementing the describeContents () function of the Parcelable interface? Most of the structure code returns 0 as an implementation. The documentation says "a bitmask indicating a set of special types of objects ordered by Parcelable." Can someone explain about this feature (maybe with an example)

+55
android
Jan 24 2018-11-11T00:
source share
1 answer

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()

+9
Jan 24 '11 at 5:50 a.m.
source share



All Articles