创建包裹对象的副本

发布于 2024-12-05 06:55:08 字数 83 浏览 0 评论 0原文

我使用包裹对象将值从一个过程传递到另一个过程。我想创建包裹对象的克隆,但是如果有人知道如何创建包裹副本,请提供解决方案,我将无法使用clone()方法。

I am using the parcel object to pass the value from one process to another. I want to create a clone of the parcel object but I am not able to use clone() method If anyone knows how to create the copy of parcel please provide the solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

内心旳酸楚 2024-12-12 06:55:08

建议的解决方案不完整并且不起作用。

这是一个可行的解决方案:(

我有一个名为 MessageDescriptor 类型的对象,我想克隆它)

Parcel parcel = Parcel.obtain();
message.writeToParcel(parcel, 0);
parcel.setDataPosition(0);
MessageDescriptor messageToBeSent = MessageDescriptor.CREATOR.createFromParcel(parcel);
parcel.recycle();

The suggested solution is incomplete and will not work.

Here's a working solution:

(I have an object called message of type MessageDescriptor which I want to clone)

Parcel parcel = Parcel.obtain();
message.writeToParcel(parcel, 0);
parcel.setDataPosition(0);
MessageDescriptor messageToBeSent = MessageDescriptor.CREATOR.createFromParcel(parcel);
parcel.recycle();
谁人与我共长歌 2024-12-12 06:55:08

假设您的对象实现了 Parcelable 接口,您应该能够执行以下操作:

SomethingParcelable myObject = new SomethingParcelable();

Parcel p = Parcel.obtain();
myObject.writeToParcel(p, 0);

//must be called after unmarshalling your data.
p.setDataPosition(0);

SomethingParcelable myClonedObject = SomethingParcelable.CREATOR.createFromParcel(p);

Assuming your object implements the Parcelable interface, you should be able to do the following:

SomethingParcelable myObject = new SomethingParcelable();

Parcel p = Parcel.obtain();
myObject.writeToParcel(p, 0);

//must be called after unmarshalling your data.
p.setDataPosition(0);

SomethingParcelable myClonedObject = SomethingParcelable.CREATOR.createFromParcel(p);
夏见 2024-12-12 06:55:08

创建副本而无需访问对象特定创建者使用的另一种方法。

 public <T extends Parcelable> T deepClone(T objectToClone) {
    Parcel parcel = null;
    try {   
        parcel = Parcel.obtain();
        parcel.writeParcelable(objectToClone, 0);
        parcel.setDataPosition(0);
        return parcel.readParcelable(objectToClone.getClass().getClassLoader());
    } finally {
        if (parcel != null) {
            parcel.recycle();
        }
    }
}

Another way to create a copy without accessing object specific CREATOR use following generic method.

 public <T extends Parcelable> T deepClone(T objectToClone) {
    Parcel parcel = null;
    try {   
        parcel = Parcel.obtain();
        parcel.writeParcelable(objectToClone, 0);
        parcel.setDataPosition(0);
        return parcel.readParcelable(objectToClone.getClass().getClassLoader());
    } finally {
        if (parcel != null) {
            parcel.recycle();
        }
    }
}
梦冥 2024-12-12 06:55:08

也适用于复制构造函数。

/**
 * Copy request passed in.
 *
 * @param request Request To clone, null is accepted, just creates a blank object
 */
public RealTimeJourneyPlanRequest(@Nullable RealTimeJourneyPlanRequest request) {
    if(request == null) return;
    // Only copy if the request past in is not null
    // Use the Parcel as its does deep cloning for us.
    final Parcel parcel = Parcel.obtain();
    request.writeToParcel(parcel, 0);
    parcel.setDataPosition(0);
    // Copy into this object
    readFromParcel(parcel);
    // Clean parcel back into object pool
    parcel.recycle();
}

Also useful for copy constructors.

/**
 * Copy request passed in.
 *
 * @param request Request To clone, null is accepted, just creates a blank object
 */
public RealTimeJourneyPlanRequest(@Nullable RealTimeJourneyPlanRequest request) {
    if(request == null) return;
    // Only copy if the request past in is not null
    // Use the Parcel as its does deep cloning for us.
    final Parcel parcel = Parcel.obtain();
    request.writeToParcel(parcel, 0);
    parcel.setDataPosition(0);
    // Copy into this object
    readFromParcel(parcel);
    // Clean parcel back into object pool
    parcel.recycle();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文