Rather simple scenario really, but I could not find anything related on Google so here goes:
class ContainerClass implements Parcelable {
List<ItemClass> _items;
(...)
public void writeToParcel( Parcel p, int args ) {
p.writeList( _items );
(...)
}
}
class ItemClass implements Parcelable {
ContainerClass _containerRef;
(...)
public void writeToParcel( Parcel p, int args ) {
p.writeParcelable( _containerRef );
(...)
}
}
This will inevitably loop and overflow the stack.
My question: How am I supposed to deal with a situation where I have to pass an object of the above types through to a new Activity.
(For CommonsWare) Parcelable implementation does indeed not seem to check for, and avoid circular dependencies.
Stacktrace with classnames replaced by above names:
08-12 10:17:45.233 5590-5590/com.package E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.StackOverflowError
at com.package.ContainerClass.writeToParcel(ContainerClass.java:139)
at android.os.Parcel.writeParcelable(Parcel.java:1254)
at com.package.ItemClass.writeToParcel(ItemClass.java:182)
at android.os.Parcel.writeParcelable(Parcel.java:1254)
at android.os.Parcel.writeValue(Parcel.java:1173)
at android.os.Parcel.writeList(Parcel.java:622)
at com.package.ContainerClass.writeToParcel(ContainerClass.java:144)
at android.os.Parcel.writeParcelable(Parcel.java:1254)
at com.package.ItemClass.writeToParcel(ItemClass.java:182)
at android.os.Parcel.writeParcelable(Parcel.java:1254)
at android.os.Parcel.writeValue(Parcel.java:1173)
at android.os.Parcel.writeList(Parcel.java:622)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…