Android differently refers to another possible circular dependency

Rather a simple script actually, but I could not find anything related to Google, so here:

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 lead to a loop and stack overflow.

My question is: how should I deal with a situation where I need to pass an object of the above types using a new action.

(For CommonsWare) The original implementation does not seem to check or require circular dependencies. Stacktrace with class names are replaced by the following 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) 
+8
android loops stack-overflow parcelable chain
source share
2 answers

I pondered the other and came up with two workarounds if someone else was in the same boat:

1) (Inspired by CommonsWare) Place a flag on each part of the chain to indicate the direction. Up through the hierarchy is a loss in the sense that all items in the ContainerClass cannot be restored.

 class ContainerClass implements Parcelable { boolean _parcelableDownHeirarchy = true; List<ItemClass> _items; (...) private ContainerClass( Parcel in ) { _items = in.readArrayList( ItemClass.class.getClassLoader() ); (...) if ( _parcelableDownHierarchy ) { for ( int i = 0; i < _items.size(); i++ ) _items.get( i ).set_container( this ); } } public void writeToParcel( Parcel p, int args ) { p.writeByte( (byte)_parcelableDownHierarchy ? 1 : 0 ); if ( _parcelableDownHeirarchy ) p.writeList( _items ); (...) } } class ItemClass implements Parcelable { boolean _parcelableDownHeirarchy = true; ContainerClass _containerRef; (...) private ItemClass( Parcel in ) { if ( !_parcelableDownHeirarchy ) { _containerRef = in.readParcelable( ContainerClass.class.getClassLoader() ); //Won't contain item in it _items list. } } public void writeToParcel( Parcel p, int args ) { p.writeByte( (byte)_parcelableDownHierarchy ? 1 : 0 ); if ( !_parcelableDownHeirarchy ) //Up the heirarchy p.writeParcelable( _containerRef ); (...) } } 

2) A Hackish workaround using the static hash table provided to each object can be uniquely identified by its possible attributes. (In my case, I have a primary key in my database in objects).

 class ContainerClass implements Parcelable { //Leave be } class ItemClass implements Parcelable { HaspMap<Long, ContainerClass> _parents = new HashMap<Long, ContainerClass>(); ContainerClass _containerRef; (...) public long get_PKhash() { /* Return unique identifier */ } private ItemClass( Parcel in ) { (...) assertTrue( (_containerRef = _parents.remove( get_PKhash() )) != null ); } public void writeToParcel( Parcel p, int args ) { (...)//Don't write _containerRef _parents.put( this.get_PKhash, _containerRef ); } } 
+1
source share

This will inevitably lead to a loop and stack overflow.

AFAIK, the distribution process does not process object pie charts . I just logged the problem to get this documentation better .

One workaround is to not do p.writeParcelable( _containerRef ); . Instead, in the ContainerClass in your ContainerClass(Parcel in) constructor (or, nevertheless, your CREATOR processes it), after reading in your _items list, _items over this list and tell each child about its parent object.

+8
source share

All Articles