看起来 Parcelable 并没有像 Serializable 那样优雅地处理循环引用。在以下示例中,Bar 的序列化工作正常,但将其写入 Parcel 会导致堆栈溢出:
I/TestRunner( 1571): java.lang.StackOverflowError
I/TestRunner( 1571): at android.os.Parcel.writeParcelable(Parcel.java:1106)
I/TestRunner( 1571): at android.os.Parcel.writeValue(Parcel.java:1029)
I/TestRunner( 1571): at com.XXX.util.ParcelableTest$Bar.writeToParcel(ParcelableTest.java:209)
I/TestRunner( 1571): at android.os.Parcel.writeParcelable(Parcel.java:1106)
I/TestRunner( 1571): at android.os.Parcel.writeValue(Parcel.java:1029)
I/TestRunner( 1571): at com.XXX.util.ParcelableTest$Baz.writeToParcel(ParcelableTest.java:246)
I/TestRunner( 1571): at android.os.Parcel.writeParcelable(Parcel.java:1106)
I/TestRunner( 1571): at android.os.Parcel.writeValue(Parcel.java:1029)
I/TestRunner( 1571): at com.XXX.util.ParcelableTest$Bar.writeToParcel(ParcelableTest.java:209)
I/TestRunner( 1571): at android.os.Parcel.writeParcelable(Parcel.java:1106)
I/TestRunner( 1571): at android.os.Parcel.writeValue(Parcel.java:1029)
public void testCircular() throws Exception {
final Bar bar = new Bar();
final Baz baz = new Baz(bar);
bar.baz = baz;
// First, serialize
final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
new ObjectOutputStream(bytes).writeObject(bar);
final ByteArrayInputStream bytesIn = new ByteArrayInputStream(bytes.toByteArray());
final Bar bar2 = (Bar) new ObjectInputStream(bytesIn).readObject();
assertNotNull(bar2);
assertNotNull(bar2.baz);
assertEquals( bar2, bar2.baz.bar );
// Now try same thing using parcelable
final Parcel p = Parcel.obtain();
p.writeValue(bar); // FAIL! StackOverflowError
p.setDataPosition(0);
final Bar bar3 = (Bar) p.readValue(Bar.class.getClassLoader());
assertNotNull(bar3);
assertNotNull(bar3.baz);
assertEquals( bar3, bar3.baz.bar );
}
protected static class Bar implements Parcelable, Serializable {
private static final long serialVersionUID = 1L;
public static final Parcelable.Creator<Bar> CREATOR = new Parcelable.Creator<Bar>() {
public Bar createFromParcel(Parcel source) {
final Bar f = new Bar();
f.baz = (Baz) source.readValue(Bar.class.getClassLoader());
return f;
}
public Bar[] newArray(int size) {
throw new UnsupportedOperationException();
}
};
public Baz baz;
public Bar() {
}
public Bar( Baz baz ) {
this.baz = baz;
}
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel dest, int ignored) {
dest.writeValue(baz);
}
}
protected static class Baz implements Parcelable, Serializable {
private static final long serialVersionUID = 1L;
public static final Parcelable.Creator<Baz> CREATOR = new Parcelable.Creator<Baz>() {
public Baz createFromParcel(Parcel source) {
final Baz f = new Baz();
f.bar = (Bar) source.readValue(Baz.class.getClassLoader());
return f;
}
public Baz[] newArray(int size) {
throw new UnsupportedOperationException();
}
};
public Bar bar;
public Baz() {
}
public Baz( Bar bar ) {
this.bar = bar;
}
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel dest, int ignored) {
dest.writeValue(bar);
}
}
我正在尝试将一些代码从使用 Serializable 移植到使用循环引用的 Parcelable。是否有使用 Parcelable 处理此问题的好策略?