16
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();
  }
}

During my Android course, the instructor used this block of code and they didn't quite explained this. How can I interpret this code? I tried reading the documentation but I failed to interpret.

Zoe
  • 27,060
  • 21
  • 118
  • 148
user9483334
  • 237
  • 2
  • 3
  • 7

2 Answers2

46

This concept is called Parcelable

A Parcelable is the Android implementation of the Java Serializable. It assumes a certain structure and way of processing it. This way a Parcelable can be processed relatively fast, compared to the standard Java serialization.

To allow your custom object to be parsed to another component they need to implement the android.os.Parcelable interface. It must also provide a static final method called CREATOR which must implement the Parcelable.Creator interface.

The code you have written will be your model class.

You can use Parcelable in Activity like :

intent.putExtra("student", new Student("1")); //size which you are storing

And to get this object :

Bundle data = getIntent().getExtras();
Student student = (Student) data.getParcelable("student");

Here Student is a model class name. replace this with yours.

In simple terms Parcelable is used to send a whole object of a model class to another page.

In your code this is in the model and it is storing int value size to Parcelable object to send and retrieve in other activity.

Reference :

Tutorial 1

Tutorial 2

Tutorial 3

Vidhi Dave
  • 5,614
  • 2
  • 33
  • 55
  • 4
    To add on in layman terms: Use Parcelable to convert an object into a sequence of bytes which can be read. transferred between Activities. – chia yongkang Nov 13 '19 at 04:15
  • 3
    I also found the explanation on this link quite useful as it also points out the caveats when implementing the Parcelable interface: _[Using Parcelable](https://guides.codepath.com/android/using-parcelable)_ – rayalois22 Jun 17 '20 at 11:53
5

--> Parcelable in Android

The Bundle object which is used to pass data to Android components is a key/value store for specialized objects. It is similar to a Map but can only contain these specialized objects

You can place the following objects types into a Bundle:

String

primitives

Serializable

Parcelable

If you need to pass your customer objects via a Bundle, you should implement the Parcelable interface.

--> Implementing Parcelable

You can create a POJO class for this, but you need to add some extra code to make it Parcelable. Have a look at the implementation.

 public class Student implements Parcelable{
        private String id;
        private String name;
        private String grade;
     
        // Constructor
        public Student(String id, String name, String grade){
            this.id = id;
            this.name = name;
           this.grade = grade;
       }
       // Getter and setter methods
       .........
       .........
       
       // Parcelling part
       public Student(Parcel in){
           String[] data = new String[3];
    
           in.readStringArray(data);
           this.id = data[0];
           this.name = data[1];
           this.grade = data[2];
      }
    
       @override
       public int describeContents(){
           return 0;
      }
    
       @Override
       public void writeToParcel(Parcel dest, int flags) {
           dest.writeStringArray(new String[] {this.id,
                                               this.name,
                                               this.grade});
       }
       public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
           public Student createFromParcel(Parcel in) {
               return new Student(in); 
           }
    
           public Student[] newArray(int size) {
               return new Student[size];
           }
       };
   }

Once you have created this class, you can easily pass objects of this class through the Intent like this, and recover this object in the target activity.

   intent.putExtra("student", new Student("1","Mike","6"));

Here, the student is the key which you would require to unparcel the data from the bundle.

   Bundle data = getIntent().getExtras();
   Student student = data.getParcelable("student");

This example shows only String types. But, you can parcel any kind of data you want. Try it out.

Rehan Khan
  • 1,031
  • 13
  • 10