1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
Classes implementing
[`android.os.Parcelable`](https://developer.android.com/reference/android/os/Parcelable.html)
must also have a non-`null` static field called `CREATOR` of a type that
implements `Parcelable.Creator`.
Classes which don't follow this spec will compile fine but might not work in
Android runtime. Depending on platform, one will observe following crash at
runtime : `android.os.BadParcelableException: Parcelable protocol requires a
Parcelable.Creator object called CREATOR`
A typical example of correct implementation:
```
public class MyParcelable implements Parcelable {
private int data;
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel out, int flags) {
out.writeInt(data);
}
public static final Parcelable.Creator<MyParcelable> CREATOR
= new Parcelable.Creator<MyParcelable>() {
public MyParcelable createFromParcel(Parcel in) {
return new MyParcelable(in);
}
public MyParcelable[] newArray(int size) {
return new MyParcelable[size];
}
};
private MyParcelable(Parcel in) {
data = in.readInt();
}
}
```
|