[英]How to write an instance of Parcelable class 'A' which contains an instance of parcelable class 'B', to Parcel from class 'B'
I have some problems with using parcelables for my Android app. 我在Android应用程序中使用可包裹货物时遇到一些问题。
I have a custom class named Transport
, which contains a bunch of RoutePoint
-instances which has been loaded from a file and saved in an array. 我有一个名为Transport
的自定义类,其中包含一堆RoutePoint
,这些RoutePoint
已从文件加载并保存在数组中。
Both the classes implements Parcelable
, because I want to pass the Transport
object, which contains RoutePoint
objects, between multiple activities. 这两个类都implements Parcelable
,因为我想在多个活动之间传递包含RoutePoint
对象的Transport
对象。
This brings a problem, because inside of each RoutePoint
object, I have a Transport
object used to tell which transport is the parent for the RoutePoint
. 这就带来了一个问题,因为在每个RoutePoint
对象的内部,都有一个Transport
对象,该对象用于告诉哪个传输是RoutePoint
的父RoutePoint
。
Pretend that I have these classes: 假设我有这些课程:
Transport
: Transport
:
public class Transport implements Parcelable
{
private RoutePoint[] points;
public Transport()
{
// Here the array is initialized
}
// Methods needed because of the Parcelable interface
}
RoutePoint
: RoutePoint
:
public class RoutePoint implements Parcelable
{
private Transport parent;
public RoutePoint(Transport parent)
{
this.parent = parent;
}
// Methods needed because of the Parcelable interface
}
When the Transport
class is written to a Parcel
, I write all the RoutePoint
s from the array to the parcel too. 当将Transport
类写入Parcel
,我也将数组中的所有RoutePoint
都写入了包裹。
When the RoutePoint
class is written to a Parcel
, I write the parent Transport
to the parcel too. 当RoutePoint
类被写入到一个Parcel
,我写的父Transport
的包裹了。
This causes a "recursive call", and a StackOverflowException
: 这将导致“递归调用”和StackOverflowException
:
Transport
writes a RoutePoint
to the Parcel
. Transport
将RoutePoint
写入Parcel
。 RoutePoint
writes the parent Transport
to the Parcel
. 该RoutePoint
写入父Transport
的Parcel
。 Transport
writes a RoutePoint
to the Parcel
. 父Transport
将RoutePoint
写入Parcel
。 I wonder how I can get around this problem, and pass the Transport
class between activities, and passing the parent
object in the RoutePoint
class in some way. 我想知道如何解决此问题,并在活动之间传递Transport
类,并以某种方式在RoutePoint
类中传递parent
对象。
If all RoutePoint
instances are always contained inside a Transport
instance, you can fix it by not serializing RoutePoint.parent, but rather setting it when you deserialize Transport. 如果所有RoutePoint
实例始终包含在Transport
实例内,则可以通过不序列化RoutePoint.parent而是在反序列化Transport时进行设置来对其进行修复。
So, when you deserialize a Transport instance, deserialize the RoutePoints, then call a setTransport
method on those RoutePoint instances and set the parent. 因此,在反序列化Transport实例时,反序列化RoutePoints,然后在这些RoutePoint实例上调用setTransport
方法并设置父对象。
Having Circular reference like that is not really a good idea from GC perspective too. 从GC的角度来看,具有这样的循环引用也不是一个好主意。 I would suggest you have one way reference and use other data structure like BiMap etc to have those relationship defined. 我建议您使用一种方法引用,并使用其他数据结构(如BiMap等)来定义这些关系。 One way is to create an Empty constructor in RoutePoint and only parcel other item except for the parent. 一种方法是在RoutePoint中创建一个Empty构造函数,然后仅打包除父项以外的其他项。 In Transport, while reading from parcel, set its reference later. 在运输中,从包裹中读取时,请稍后设置其参考。 Hope this makes sense. 希望这是有道理的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.