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?

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top