如何将处于双向一对一关系中的两个对象打包?
How to parcel two object which are in a bidirectional one-to-one relationship?
我有两个这样的对象,我想通过 Intent
实现 Parcelable
接口发送:
class Foo implements Parcelable
{
private Bar bar;
public void writeToParcel(Parcel dest, int flags)
{
dest.writeParcelable(bar, flags);
}
}
class Bar implements Parcelable
{
private Foo foo;
public void writeToParcel(Parcel dest, int flags)
{
dest.writeParcelable(foo, flags);
}
}
如何正确实现 Parcelable
接口?
我会这样做:
在Foo
的writeToParcel
中class写入Foo
实例的所有字段没有Bar
字段和Bar
实例的所有字段没有Foo
字段。
然后在Foo(Parcel in)
中读取Foo
的所有字段并使用Bar
的字段创建Bar
实例和link两个对象。
我有两个这样的对象,我想通过 Intent
实现 Parcelable
接口发送:
class Foo implements Parcelable
{
private Bar bar;
public void writeToParcel(Parcel dest, int flags)
{
dest.writeParcelable(bar, flags);
}
}
class Bar implements Parcelable
{
private Foo foo;
public void writeToParcel(Parcel dest, int flags)
{
dest.writeParcelable(foo, flags);
}
}
如何正确实现 Parcelable
接口?
我会这样做:
在Foo
的writeToParcel
中class写入Foo
实例的所有字段没有Bar
字段和Bar
实例的所有字段没有Foo
字段。
然后在Foo(Parcel in)
中读取Foo
的所有字段并使用Bar
的字段创建Bar
实例和link两个对象。