简体   繁体   中英

What is Parcelable in android

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.

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

--> 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM