How to read/write a boolean when implementing the Parcelable interface?

后端 未结 12 1279
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-29 14:33

I\'m trying to make an ArrayList Parcelable in order to pass to an activity a list of custom object. I start writing a myObjectList cl

12条回答
  •  野性不改
    2020-11-29 15:10

    There are many examples in the Android (AOSP) sources. For example, PackageInfo class has a boolean member requiredForAllUsers and it is serialized as follows:

    public void writeToParcel(Parcel dest, int parcelableFlags) {
        ...
        dest.writeInt(requiredForAllUsers ? 1 : 0);
        ...
    }
    
    private PackageInfo(Parcel source) {
        ...
        requiredForAllUsers = source.readInt() != 0;
        ...
    }
    

提交回复
热议问题