Android: Difference between Parcelable and Serializable?
In Android we cannot just pass objects to activities. To do this the objects must either implement Serializable
or Parcelable
interface.
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 creates a lot of temporary objects and causes quite a bit of garbage collection. However, Serializable
interface is easier to implement.
Look at the example below (Serializable):
// MyObjects Serializable classimport 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<String> address; public MyObjects(String name, int age, ArrayList<String> address) { super(); this.name = name; this.age = age; this.address = address; } public ArrayList<String> getAddress() { if (!(address == null)) return address; else return new ArrayList<String>(); } public String getName() { return name; } // return age public int getAge() { return age; }}
// MyObjects instanceMyObjects mObjects = new MyObjects("name", "age", "Address array here");// Passing MyObjects instance via intentIntent mIntent = new Intent(FromActivity.this, ToActivity.class);mIntent.putExtra("UniqueKey", mObjects);startActivity(mIntent);
// Getting MyObjects instanceIntent 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 classimport java.util.ArrayList;import android.os.Parcel;import android.os.Parcelable;public class MyObjects implements Parcelable { private int age; private String name; private ArrayList<String> address; public MyObjects(String name, int age, ArrayList<String> 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<String> getAddress() { if (!(address == null)) return address; else return new ArrayList<String>(); } public static final Creator<MyObjects> CREATOR = new Creator<MyObjects>() { @Override public MyObjects[] newArray(int size) { return new MyObjects[size]; } @Override public MyObjects createFromParcel(Parcel source) { return new MyObjects(source); } };}
// MyObjects instanceMyObjects mObjects = new MyObjects("name", "age", "Address array here");// Passing MyOjects instanceIntent mIntent = new Intent(FromActivity.this, ToActivity.class);mIntent.putExtra("UniqueKey", mObjects);startActivity(mIntent);
// Getting MyObjects instanceIntent mIntent = getIntent();MyObjects workorder = (MyObjects) mIntent.getParcelableExtra("UniqueKey");
You can pass ArrayList
of Parcelable objects as below:
// Array of MyObjectsArrayList<MyObjects> mUsers;// Passing MyOjects instanceIntent mIntent = new Intent(FromActivity.this, ToActivity.class);mIntent.putParcelableArrayListExtra("UniqueKey", mUsers);startActivity(mIntent);
// Getting MyObjects instanceIntent mIntent = getIntent();ArrayList<MyObjects> mUsers = mIntent.getParcelableArrayList("UniqueKey");
Conclusion
Parcelable
is faster thanSerializable
interfaceParcelable
interface takes more time to implement compared toSerializable
interfaceSerializable
interface is easier to implementSerializable
interface creates a lot of temporary objects and causes quite a bit of garbage collectionParcelable
array can be passed via Intent in android
Serializable is a standard Java interface. You simply mark a class Serializable by implementing the interface, and Java will automatically serialize it in certain situations.
Parcelable is an Android specific interface where you implement the serialization yourself. It was created to be far more efficient that Serializable, and to get around some problems with the default Java serialization scheme.
I believe that Binder and AIDL work with Parcelable objects.
However, you can use Serializable objects in Intents.
If you want to be a good citizen, take the extra time to implement Parcelable since it will perform 10 times faster and use less resources.
However, in most cases, the slowness of Serializable won’t be noticeable. Feel free to use it but remember that serialization is an expensive operation so keep it to a minimum.
If you are trying to pass a list with thousands of serialized objects, it is possible that the whole process will take more than a second. It can make transitions or rotation from portrait to lanscape feel very sluggish.
Source to this point: http://www.developerphil.com/parcelable-vs-serializable/