File: ParcelableCreator.md

package info (click to toggle)
error-prone-java 2.18.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 23,204 kB
  • sloc: java: 222,992; xml: 1,319; sh: 25; makefile: 7
file content (40 lines) | stat: -rw-r--r-- 1,211 bytes parent folder | download | duplicates (2)
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();
     }
 }
```