2

explain what meaning of Parcelable.creator?

 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];
     }
 };
Umang Kothari
  • 3,674
  • 27
  • 36

2 Answers2

2

Parcelable objects allow you to serialise and deserialise on activity or fragment stop / start and it is Faster than Java serialisable.

The static CREATOR class creates your object from a Parcel via the createFromParcel method that takes in a parcel and passes it to a constructor in your class that does the grunt work.

The newArray method allows an array of your objects to be parcelled. Answer

And here is a good example of it Link

Community
  • 1
  • 1
Sami Eltamawy
  • 9,874
  • 8
  • 48
  • 66
1

Parcelable.Creator creates instances of your MyParcelable from a Parcel object. For example the typic Parcelable should look like this :

 public class MyParcelable implements Parcelable {
     private int mData;

     public int describeContents() {
         return 0;
     }

     public void writeToParcel(Parcel out, int flags) {
         out.writeInt(mData);
     }

     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) {
         mData = in.readInt();
     }
 }

For more information you should check the official documentation :

hardartcore
  • 16,886
  • 12
  • 75
  • 101