Serializable vs Parcelable

In Android we know that we cannot just pass objects to activities. The objects must be either implements Serializable or Parcelable interface to do this.

Serializable

Serializable is a standard Java interface. You can just implement Serializable interface and add override methods.The problem with this approach is that reflection is used and it is a slow process. This method create a lot of temporary objects and cause quite a bit of garbage collection. Serializable interface is easier to implement.

Look at the example below (Serializable)

 

//MyObjects Serializable class

import java.io.Serializable;
import java.util.ArrayList;
import java.util.TreeMap;

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

public class MyObjects implements Serializable {

private String name;
private int age;

public ArrayList address;

public MyObjects(String name, int age, ArrayList address) {
    super();
    this.name = name;
    this.age = age;
    this.address = address;
}

public ArrayList getAddress() {
    if (!(address == null))
        return address;
    else
        return new ArrayList();
}

public String getName() {
    return name;

}

public String getAge() {
    return age;
}

}



//MyObjects instance
MyObjects mObjects = new MyObjects("name","age","Address array here");

//Passing MyObjects instance via intent
Intent mIntent = new Intent(FromActivity.this, ToActivity.class);
mIntent.putExtra("UniqueKey", mObjects);
startActivity(mIntent);


//Getting MyObjects instance
Intent mIntent = getIntent();
MyObjects workorder = (MyObjects) mIntent.getSerializableExtra("UniqueKey");

Parcelable

Parcelable process is much faster than serializable. One of the reasons for this is that we are being explicit about the serialization process instead of using reflection to infer it. It also stands to reason that the code has been heavily optimized for this purpose.

Look at the example below (Parcelable)

//MyObjects Parcelable class

import java.util.ArrayList;

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

public class MyObjects implements Parcelable {

private int age;
private String name;

private ArrayList address;

public MyObjects(String name, int age, ArrayList address) {
    this.name = name;
    this.age = age;
    this.address = address;

}

public MyObjects(Parcel source) {
    age = source.readInt();
    name = source.readString();
    address = source.createStringArrayList();
}

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(age);
    dest.writeString(name);
    dest.writeStringList(address);

}

public int getAge() {
    return age;
}

public String getName() {
    return name;
}

public ArrayList getAddress() {
    if (!(address == null))
        return address;
    else
        return new ArrayList();
}

public static final Creator CREATOR = new Creator() {
    @Override
    public MyObjects[] newArray(int size) {
        return new MyObjects[size];
    }

    @Override
    public MyObjects createFromParcel(Parcel source) {
        return new MyObjects(source);
    }
};

}





MyObjects mObjects = new MyObjects("name","age","Address array here");

//Passing MyOjects instance
Intent mIntent = new Intent(FromActivity.this, ToActivity.class);
mIntent.putExtra("UniqueKey", mObjects);
startActivity(mIntent);


//Getting MyObjects instance
Intent mIntent = getIntent();
MyObjects workorder = (MyObjects) mIntent.getParcelable("UniqueKey");




//You can pass Arraylist of Parceble obect as below

//Array of MyObjects
ArrayList mUsers;

//Passing MyOjects instance
Intent mIntent = new Intent(FromActivity.this, ToActivity.class);
mIntent.putParcelableArrayListExtra("UniqueKey", mUsers);
startActivity(mIntent);


//Getting MyObjects instance
Intent mIntent = getIntent();
ArrayList mUsers = mIntent.getParcelableArrayList("UniqueKey");

 

Conclusion.

  1. Parcelable is faster than serializable interface
  2. Parcelable interface takes more time for implemetation compared to serializable interface
  3. serializable interface is easier to implement
  4. serializable interface create a lot of temporary objects and cause quite a bit of garbage collection
  5. Parcelable array can be pass via Intent in android

— taken from sof