Difference between Parcel able and Serializable in android

AndroidMobile DevelopmentApps/Applications

This example demonstrate about Difference between Parcel able and Serializable in android

Serializable

Serializable is a markable interface or we can call as empty interface. It doesn’t have any pre-implemented methods. Serializable is going to convert object to byte stream. So user can pass the data between one activity to another activity. The main advantage of serializable is creation and passing data is very easy but it is a slow process compare to parcelable.

A simple example of serializable as shown below –

import java.io.Serializable;
class serializableObject implements Serializable {
   String name;
   public serializableObject(String name) {
      this.name = name;
   }
   public String getName() {
      return name;
   }
}

Parcelable

Parcel able is faster than serializable. Parcel able is going to convert object to byte stream and pass the data between two activities. Writing parcel able code is little bit complex compare to serialization. It doesn’t create more temp objects while passing the data between two activities.

A simple example of Parcel able as shown below –

import android.os.Parcel;
import android.os.Parcelable;
class parcleObject implements Parcelable {
   private String name;
   protected parcleObject(Parcel in) {
      this.name = in.readString();
   }
   public parcleObject(String name){
      this.name = name;
   }
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   public static final Creator<parcleObject> CREATOR = new Creator<parcleObject>() {
      @Override
      public parcleObject createFromParcel(Parcel in) {
         return new parcleObject(in);
      }
      @Override
      public parcleObject[] newArray(int size) {
         return new parcleObject[size];
      }
   };
   @Override
   public int describeContents() {
      return 0;
   }
   @Override
   public void writeToParcel(Parcel dest, int flags) {
      dest.writeString(this.name);
   }
}
raja
Published on 11-Apr-2019 13:17:20
Advertisements