Skip to content

Instantly share code, notes, and snippets.

@PPartisan
Created January 9, 2016 14:48
Show Gist options
  • Save PPartisan/1ed994c2717ab25bfa9a to your computer and use it in GitHub Desktop.
Save PPartisan/1ed994c2717ab25bfa9a to your computer and use it in GitHub Desktop.
SparseBooleanArray that is also Parcelable
/**
* SparseBooleanArray that is also Parcelable. Had to put this together so I could pass this to a
* {@code Fragment} bundle.
*/
public class ParcelableSparseBooleanArray extends SparseBooleanArray implements Parcelable {
public ParcelableSparseBooleanArray(){
super();
}
protected ParcelableSparseBooleanArray(Parcel in) {
boolean[] values = in.createBooleanArray();
int[] keys = in.createIntArray();
for (int i = 0; i < values.length; i++) {
append(keys[i], values[i]);
}
}
public static final Creator<ParcelableSparseBooleanArray> CREATOR = new Creator<ParcelableSparseBooleanArray>() {
@Override
public ParcelableSparseBooleanArray createFromParcel(Parcel in) {
return new ParcelableSparseBooleanArray(in);
}
@Override
public ParcelableSparseBooleanArray[] newArray(int size) {
return new ParcelableSparseBooleanArray[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
boolean[] values = new boolean[size()];
int[] keys = new int[size()];
for (int i = 0; i < size(); i++) {
values[i] = valueAt(i);
keys[i] = keyAt(i);
}
dest.writeBooleanArray(values);
dest.writeIntArray(keys);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment