Skip to content
FrantisekGazo edited this page Dec 7, 2016 · 1 revision

parcel module provides @Parcel and @ParcelIgnore annotation.

@Parcel

Annotation for generating Parcelable implementation.

Without using this library you would have to write this:

public class MyClass implements Parcelable {

     String text;
     int number;
     boolean flag;
     double[] doubleArray;

     protected MyClass(Parcel in) {
         text = in.readString();
         number = in.readInt();
         flag = in.readByte() != 0;
         doubleArray = in.createDoubleArray();
     }

     public static final Creator<MyClass> CREATOR = new Creator<MyClass>() {
         @Override
         public MyClass createFromParcel(Parcel in) {
             return new MyClass(in);
         }

         @Override
         public MyClass[] newArray(int size) {
             return new MyClass[size];
         }
     };

     @Override
     public int describeContents() {
         return 0;
     }

     @Override
     public void writeToParcel(Parcel dest, int flags) {
         dest.writeString(text);
         dest.writeInt(number);
         dest.writeByte((byte) (flag ? 1 : 0));
         dest.writeDoubleArray(doubleArray);
     }
}

But with this library you can write this:

@blade.Parcel
public class MyClass implements Parcelable {

     String text;
     int number;
     boolean flag;
     double[] doubleArray;

     protected MyClass(Parcel in) {
     }

     @Override
     public int describeContents() {
         return 0;
     }

     @Override
     public void writeToParcel(Parcel dest, int flags) {
     }
}

Class with @Parcel has to:

  • implement Parcelable interface (empty methods are enough)
  • contain constructor with parameter of type Parcel

Getter and setter is required for every private or protected field!

@ParcelIgnore

Use this annotation if you want Blade to ignore some field, when writing/reading instance of given class to/from parcel.

@blade.Parcel
public class MyClass implements Parcelable {

     int number; // this field will be processed
     @blade.ParcelIgnore
     String text; // this field will be ignored

     protected MyClass(Parcel in) {
     }

     @Override
     public int describeContents() {
         return 0;
     }

     @Override
     public void writeToParcel(Parcel dest, int flags) {
     }
}
Clone this wiki locally