How to use Parcel.readBooleanArray()?
I'm trying to use the readB开发者_运维问答ooleanArray from android.os.Parcel, but readBooleanArray returns void and therefor it's unclear to me how to use this method.
I'm using the following method to write something to the Parcel:
public void writeToParcel(Parcel out, int flags) {
out.writeBooleanArray(new boolean[] {value});
}
How should this value be obtained in the Parcelable constructor?
I believe you need to pass a boolean[], the values in the Parcel will be copied to that, then you read from that array.
Sample code:
boolean[] myBooleanArr = new boolean[1];
parcel.readBooleanArray(myBooleanArr);
boolean value = myBooleanArr[0];
If you have a real boolean array, not only one value, and you don't know the length of the array, then you can use createBooleanArray()
instead of readBooleanArray(boolean[])
. It will return a new array which is the same you put into the parcel with writeBooleanArray(boolean[])
.
精彩评论