I have an abstract class A
which implements Parcelable.
I have a class B
and a class C
who both extend A
.
How can I make them parcelable?
Of cause I could chain it and provide a CREATOR
both in A
and B
like suggested in many posts. But since I have other Objects who store the A-B-C
classes and implement Parcelable themselfes, that approach seems not to be working because when I want to pass an ArrayList of A
I would have to use the CREATOR
in the typed list by
ArrayList<A> elements = new ArrayList<>();
in.readTypedList(elements , B.CREATOR); // B.CREATOR? C.CREATOR???
Which obviously makes no sense. So how can I properly make A Parcelable?
I.e I want to make this class Parcelable so I can refer to A in a common way.
A)
public abstract class A implements Parcelable {
final String globalVar;
public A(String globalVar) {
this.globalVar = globalVar;
}
}
B)
public class B extends A {
String bVar;
public B(String global, String bVar) {
super(global);
this.bVar = bVar;
}
private B(Parcel in) {
super(in.readString());
this.bVar = in.readString();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(bVar);
}
}
C)
public class C extends A {
String cVar;
public C(String global, String cVar) {
super(global);
this.cVar = cVar;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(cVar);
}
}