Difference between Parcelable and Serializable in android

Serializable

Serializable may be a markable interface or we will decision as an empty interface. It doesn’t have any pre-implemented ways. Serializable goes to convert Associate in Nursing object to byte stream. therefore the user will pass the info between one activity to a different activity. the most advantage of serializable is that the creation and spending information is incredibly simple however it’s a slow method compare to parcelable.

A simple example of serializable as shown below –

import java.io.Serializable;

class serializableRepo implements Serializable {
   String name;
   public serializableRepo(String name) {
      this.name = name;
   }
   public String getName() {
      return name;
   }
}

Parcelable

Parcel ready is quicker than serializable. Parcel ready goes to convert object to byte stream and pass the info between 2 activities. Writing parcel ready code is small bit advanced compare to serialization. It doesn’t produce a lot of worker objects whereas passing the info between 2 activities.

A simple example of Parcel ready as shown below –

import android.os.Parcel;
import android.os.Parcelable;

class parcleModel implements Parcelable {

   private String name;

   protected parcleModel(Parcel in) {
      this.name = in.readString();
   }

   public parcleModel(String name) {
      this.name = name;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

   public static final Creator<parcleModel> CREATOR = new Creator<parcleModel>() {
      @Override
      public parcleModelcreateFromParcel(Parcel in) {
         return new parcleRepo(in);
      }
      @Override
      public parcleModel[] newArray(int size) {
         return new parcleRepo[size];
      }
   };

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

   @Override
   public void writeToParcel(Parcel dest, int flags) {
      dest.writeString(this.name);
   }
}

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories