1

I am new to android and am having a bit of trouble wrapping my head around the Parcelable interface.

I eventually found this: https://stackoverflow.com/a/2141166/6647053

The point made in the above answer is that when passing an object to an activity, this:

intent.putExtra("object", parcelableObject);

performs much better than this:

intent.putExtra("object", serializableObject);

My question is: Would there be any performance benefit to using a Parcel's read / write Serializable methods within the Parcelable (as opposed to just using a serializable object with intent.putExtra)? Why / Why not?

Example:

public class MyParcelable implements Serializable, Parcelable {

    /* Some Custom Object Stuff Here */

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

    @Override
    public void writeToParcel(Parcel parcel, int flags) {
        parcel.writeSerializable(this);
    }

    public static final Parcelable.Creator<MyParcelable> CREATOR = new Parcelable.Creator<MyParcelable>() {
        public MyParcelable createFromParcel(Parcel parcel) {
            return parcel.readSerializable();
        }

        public MyParcelable[] newArray(int size) {
            return new MyParcelable[size];
        }
    };
}
Community
  • 1
  • 1
Zachary
  • 324
  • 1
  • 13

1 Answers1

1

There is no benefit to writing this: parceling will be as slow as serializing.

In ordinary Java, Externalizable can perform better than Serializable, because you supply your own readExternal(ObjectInput in) and writeObject(ObjectOutput out) in which you are expected to manually serialize your fields instead of relying on the JVM to introspect and automatically do it for you. Android's Parcelable serves a similar purpose.

ephemient
  • 198,619
  • 38
  • 280
  • 391