6

I'm trying to use the readBooleanArray 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?

Joost
  • 1,426
  • 2
  • 16
  • 39

2 Answers2

13

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];
C0deAttack
  • 24,419
  • 18
  • 73
  • 81
  • Have you ever faced problem when you need to parcel few booleans... i have. Could You please help me> ?http://stackoverflow.com/questions/13463727/readbooleanarray-throws-runtimeexceptionbad-array-lengths. Thanks. – Roger Alien Nov 23 '12 at 22:03
2

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[]).

hunyadym
  • 2,213
  • 25
  • 39