Search code examples
androidparcelable

Parcelable writeXXX versus writeBundle


When implementing the Parcelable interface, I always struggle to see the point of the guideline.

Lets take a simple example, when my Parcelable object has only 3 fields : a String, a boolean and a List.

The useful code should look like this :

private String myString;
private boolean myBool;
private List<String> myList

private MyParcelableObject(Parcel in) {
    myString = in.readString();
    myBool = in.readByte() != 0;
    myList = in.readArrayList(String.class.getClassLoader());
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(myString);
    dest.writeByte((byte) (myBool ? 1 : 0));
    dest.writeList(myList);
}

However, this order-based registering seems very weird to me.

But since you can write a Bundle to the parcel, why not write something like this ?

private final static String KEY_STR = "key_str", 
    KEY_BOOL = "key_bool", 
    KEY_LIST = "key_list";

private MyParcelableObject(Parcel in) {
    Bundle b =in.readreadBundle(Bundle.class.getClassLoader());
    myString = b.getString(KEY_STR);
    myBool = b.putBoolean(KEY_BOOL);
    myList = b.getStringArrayList(KEY_LIST);
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    Bundle b = new Bundle();
    b.putString(KEY_STR, myString);
    b.putBoolean(KEY_BOOL, myBool);
    b.putStringArrayList(KEY_LIST, myList);
    dest.writeBundle(b);
}

The order-based registering is now key-based and there seems to be very few methods Parcel has and Bundle has not.

But since the 1st way is the most seen on the web (and somehow in the official javadoc) I assume this is the "good" way, so what are its benefits over the 2nd way ?


Solution

  • Parcelables are more performant than Bundles.

    Exactly because a Parcelable is strict in the ordering of the fields it does not need much logic to read them and thus is pretty quick. While in contrast a Bundle is very flexible in what it can contain, reading from it means it needs more logic to support the flexibility and is less quick. When you put a Bundle in a Parcelable then the Parcelable looses its performance advantage.

    The above is not entirely true. You can build any logic that you want into a Parcelable and thus you could program it to have as much or little flexibility as you want and thereby choose your own trade off against performance. But that comes at the price of more code complexity and possibly bugs. That does not seem like a good choice and, I think, not many go down that route.

    To give you more to think about: Android also supports the standard Java Serializable interface. This is often a very good choice for classes that only have fields with primitive types or other Serializable types. In that case you just add implements Serializable on the class and it works, but still not as fast as Parcelables.

    Remember before making a decision about performance: Measure it before optimising anything because the performance may already be good enough for your purposes.