Do you have a Parcel to send?
I have been working on the Android platform recently. I am truly enjoying the time I am spending building an app on the platform. One useful concept is the Parcel interface.
Java already has a Serializable interface to marshal and unmarshal objects, so why use Parcelable? Good Question, Java's serializable will try to marshall everything in your object using a lot of reflection, which can be slower. Parcelable on the other hand makes you, the programmer, marshal and unmarshal your object manually. This method does not use reflection making it theoretically faster.
So lets see an example of implementing Parcelable.
class Foo implements Parcelable { int testInt; boolean testBool; String testStr; public int describeContent() { return 0; } public void writeToParcel(Parcel pc, int flags) { pc.writeInt(testInt); // There is no writeBoolean this is an alternative pc.writeByte((byte)(testBool ? 1 : 0)); pc.writeString(testStr); } public static final Parcelable.Creator<Foo> CREATOR = new Parcelable.Creator<Foo>() { public Foo createFromParcel(Parcel pc) { return new Foo(pc); } public Foo[] newArray(int size) { return new Foo[size]; } }; public Foo(Parcel pc) { testInt = pc.readInt(); testBool = (pc.readByte() == 1); testStr = pc.readString(); } }
Some important things to note.
The order of how you write the variables into the parcel must also be the order you read them out of the parcel. This is the manual marshaling and unmarshaling we talked about before.
Parcelable.Creator implementation is required.
Hope this helps explain Parcelable.