You need to write the array using the Parcel.writeTypedArray()
method and read it back with the Parcel.createTypedArray()
method, like so:
MyClass[] mObjList;
public void writeToParcel(Parcel out) {
out.writeTypedArray(mObjList, 0);
}
private void readFromParcel(Parcel in) {
mObjList = in.createTypedArray(MyClass.CREATOR);
}
The reason why you shouldn’t use the readParcelableArray()
/writeParcelableArray()
methods is that readParcelableArray()
really creates a Parcelable[]
as a result. This means you cannot cast the result of the method to MyClass[]
. Instead you have to create a MyClass
array of the same length as the result and copy every element from the result array to the MyClass
array.
Parcelable[] parcelableArray =
parcel.readParcelableArray(MyClass.class.getClassLoader());
MyClass[] resultArray = null;
if (parcelableArray != null) {
resultArray = Arrays.copyOf(parcelableArray, parcelableArray.length, MyClass[].class);
}