There is an interface DrawingElement
(extending Parcelable
) with multiple implementations. There is a field List<DrawingElement> elements
in another class, which implements Parcelable
. So the List should be written to the Parcel, but the method for reading Parcelable ArrayList requires a concrete class for referencing the concrete implementation of a Creator and this is not working: in.createTypedArrayList(DrawingElement.CREATOR)
.
public interface DrawingElement extends Parcelable {
}
public class FirstDrawingElementImpl implements DrawingElement {
@Override
public void writeToParcel(Parcel dest, int flags) {
// done easily.
}
}
public class SecondDrawingElementImpl implements DrawingElement {
@Override
public void writeToParcel(Parcel dest, int flags) {
// done easily.
}
}
public class DrawingElementsContainer implements Parcelable {
private List<DrawingElement> elements;
@Override
public void writeToParcel(Parcel dest, int flags) {
// TODO:
}
}
Any suggestions?
UPDATE:
If I had to use the @pskink suggestion, I had to do weird casting like:
protected Drawing(Parcel in) {
DrawingElement[] parcelables = (DrawingElement[]) in.readParcelableArray(DrawingElement.class.getClassLoader());
elements = new ArrayList<>(Arrays.asList(parcelables));
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeParcelableArray(elements.toArray(new Parcelable[elements.size()]), flags);
}
dest.writeList(elements)
is the obvious decision, but I got confused by passing an interface classloader.