Question

I happen to know what if my class Info which implements Parcelable has empty writeToParcel() method, Bundle.putParcelableArrayList() and Bundle.getParcelableArrayList() methods still work on ArrayList< Info>.

public class Info implements Parcelable
{
    public int row;
    public int column;

    public int describeContents()
    {
        return 0;
    }

    public void writeToParcel(Parcel out, int flags)
    {  
        // Left empty!
    }
}

So the question is why? When I really should implement writeToParcel method as API documentation and books instruct?

Was it helpful?

Solution

The Android Bundle class does not follow the same protocol that is followed during IPC marshaling. Through reflection, a class that implements Bundle will simply read & write the Parcelable object into its own internal mapping. So, whatever properties you define in your Parcelable derived class will be used.

On the contrary, if you're using the IPC Marshalling protocol - e.g. passing your Info object from one activity to another - this is when you need to implement a specific writeToParcelable method and the appropriate constructor.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top