使用 Parcelable 在活动之间传递高度嵌套的类 [英] Using Parcelable to pass highly nested classes between Activities

查看:67
本文介绍了使用 Parcelable 在活动之间传递高度嵌套的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我想在 Intent 中存储类型为 MyObject 的自定义对象.这样做的方法是让MyObject 实现Parcelable.如果 MyObject 的字段之一也是 Widget 类型的自定义对象,那么显而易见的事情就是让 Widget 实现 Parcelable 也是.

Suppose I want to store a custom object of type MyObject in an Intent. The way to do this is to make MyObject implement Parcelable. If one of the fields of MyObject is also a custom object of type Widget the obvious thing to do is to make Widget implement Parcelable too.

问题是在实现Parcelable 时涉及大量的样板文件.你可以通过不让 Widget 实现 Parcelable 来解决这个问题,而只是给它一个带有 Parcel 的构造函数和一个方法 writeToParcel 如下:

The trouble is that there is a huge amount of boilerplate involved when implementing Parcelable. You can get around this by not making Widget implement Parcelable but instead just giving it a constructor taking a Parcel and a method writeToParcel as follows:

public final class Widget {

    private final int a;
    private final String b;

    Widget(Parcel in) {
        a = in.readInt();
        b = in.readString();
    }

    void writeToParcel(Parcel out) {
        out.writeInt(a);
        out.writeString(b);
    }
}

然后您可以在 Parcelable 对象中有一个 Widget 字段,如下所示:

You can then have a Widget field in a Parcelable object as follows:

public class MyObject implements Parcelable {

    private final int x;
    private final Widget w;

    MyObject(int x, Widget w) {
        this.x = x;
        this.w = w;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel out, int flags) {
        out.writeInt(x);
        w.writeToParcel(out);
    }

    public static final Parcelable.Creator<MyObject> CREATOR
            = new Parcelable.Creator<MyObject>() {
        @Override
        public MyObject createFromParcel(Parcel in) {
            return new MyObject(in.readInt(), new Widget(in));
        }
        @Override
        public MyObject[] newArray(int size) {
            return new MyObject[size];
        }
    };
}

这是一种可以接受的方法吗?在一个项目中有许多自定义类可以被写入和读取 Parcel 而没有它们实际实现 Parcelable 是否被认为是 unidiomatic android?或者,我使用 Parcelable 传递具有许多自定义类型字段的复杂对象(这些字段又具有许多自定义类型字段等)这一事实是否表明我不应该使用 Parcelable 首先是什么?

Is this an acceptable approach? Is it considered unidiomatic android to have many custom classes in a project that can be written to and read from Parcels without them actually implementing Parcelable? Or does the fact that I am using a Parcelable to pass complex objects with many fields of custom types (which in turn have many fields of custom type etc etc), indicate that I shouldn't be using Parcelable in the first place?

推荐答案

我会(并且确实)使用 Parceler:https://github.com/johncarl81/parceler

I would (and did) go with Parceler: https://github.com/johncarl81/parceler

Parceler 是一个代码生成库,用于生成 AndroidParcelable 样板源代码.您不再需要实施Parcelable 接口,writeToParcel() 或 createFromParcel() 或公共静态最终 CREATOR.您只需使用@Parcel 和 Parceler 完成剩下的工作.

Parceler is a code generation library that generates the Android Parcelable boilerplate source code. No longer do you have to implement the Parcelable interface, the writeToParcel() or createFromParcel() or the public static final CREATOR. You simply annotate a POJO with @Parcel and Parceler does the rest.

它真的很容易使用.

这篇关于使用 Parcelable 在活动之间传递高度嵌套的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆