简体   繁体   English

什么时候在android中使用parcelable?

[英]When to use parcelable in android?

I'm used to develop in ios where I never need to make a model parcelable so the concept is not very clear to me. 我习惯于在ios中开发我永远不需要制作可模仿的模型,所以这个概念对我来说不是很清楚。 I have a class "game" like: 我有一个类“游戏”,如:

//removed the method to make it more readable.
public class Game implements Parcelable {
    private int _id;
    private ArrayList<Quest> _questList;
    private int _numberOfGames;
    private String _name;
    private Date _startTime;

    public Game(String name, ArrayList<Quest> quests, int id){
        _name = name;
        _questList = quests;
        _numberOfGames = quests.size();
        _id = id;
    }
}

I want to start an activity and pass the game object to the activity with my intent, but it turned out you can't pass custom objects by default but they need to be parcelable. 我想开始一个活动,并以我的意图将游戏对象传递给活动,但事实证明,默认情况下你不能传递自定义对象,但它们需要是可以分配的。 So I've added: 所以我添加了:

public static final Parcelable.Creator<Game> CREATOR
        = new Parcelable.Creator<Game>() {
    public Game createFromParcel(Parcel in) {
        return new Game(in);
    }

    public Game[] newArray(int size) {
        return new Game[size];
    }
};
private Game(Parcel in) {
    _id = in.readInt();
    _questList = (ArrayList<Quest>) in.readSerializable();
    _numberOfGames = in.readInt();
    _name = in.readString();
    _startTime = new Date(in.readLong());
}

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

@Override
public void writeToParcel(Parcel out, int flags) {
    out.writeInt(_id);
    out.writeSerializable(_questList);
    out.writeInt(_numberOfGames);
    out.writeString(_name);
    out.writeLong(_startTime.getTime());
}

But now I get the warning that the custom arraylist _questList is not parcelable Game. 但现在我得到警告,自定义arraylist _questList不是parcelable游戏。

Quest is an abstract class so it cant implement . Quest是一个抽象类,因此无法实现

   public static final Parcelable.Creator<Game> CREATOR = new Parcelable.Creator<Game>() {
    public Game createFromParcel(Parcel source) {
        return new Game(source);
    }

    public Game[] newArray(int size) {
        return new Game[size];
    }
};

So my question is: When do I need to implement parcelable, do I have to add it to every custom object I want to pass (even if in another custom object)? 所以我的问题是:我什么时候需要实现parcelable,我是否必须将它添加到我想传递的每个自定义对象(即使在另一个自定义对象中)? I can't imagine they don't have something easier for android to pass a custom object with an array list of custom objects. 我无法想象他们没有更容易的东西来传递带有自定义对象数组列表的自定义对象。

As you already discovered if you want to send own data via Intent you need to make it possible. 正如您已经发现的,如果您想通过Intent发送自己的数据,您需要使其成为可能。 On Android it's recommended to use Parcelable . 在Android上,建议使用Parcelable You may implement this interface yourself or use existing tools like Parceler or Parcelable Please . 您可以自己实现此界面,也可以使用ParcelerParcelable Please等现有工具。 Note: these tools comes with some limitation so ensure you know them as sometimes it may be cheaper to implement Parcelable by hand instead of writing code to work it around. 注意:这些工具有一些限制因此请确保您了解它们,因为有时手动实现Parcelable可能更便宜,而不是编写代码来处理它。

is parcelable to only way possible 只有可能的方式可以包容

No. You can use Serializable (also with Parcels), but Parcelable is the way to go on Android as it is faster and it is how it's done on platform level. 不可以。您可以使用Serializable (也可以使用Parcel),但Parcelable可以用于Android,因为它更快,而且它是在平台级别上完成的。

Let's say Parcelable is something like Optimized Serializable, designed for Android and Google recommends to use Parcelable over Serializable. 我们假设Parcelable类似于Optimized Serializable,专为Android设计 ,Google建议使用Parcelable over Serializable。 Android OS uses Parcelable iteself (for example: SavedState for Views). Android操作系统使用Parcelable iteself(例如:SavedState用于查看)。 Implementing Parcelable by hand is a bit of pain, so there are some helpful solutions: 手动实现Parcelable有点痛苦,所以有一些有用的解决方案:

  • Android Parcerable Generator. Android Parcerable Generator。 IntelliJ plugin which implements Parcelable stuff for you (adds constructor, CREATOR inner class, implements methods etc.) for your data class. IntelliJ插件,为您的数据类实现Parcelable东西(添加构造函数, CREATOR内部类,实现方法等)。 You can get it here . 你可以在这里得到它。 在此输入图像描述

  • Parceler. Parceler。 Annotation-based code generation framework. 基于注释的代码生成框架。 You have to use @Parcel annotation for you data class, and some helper methods. 您必须为数据类和一些辅助方法使用@Parcel注释。 More info here . 更多信息在这里 在此输入图像描述

  • Parcelable Please. Parcelable请。 Annotation-based code generation framework shipped along with the IntelliJ plugin. 基于注释的代码生成框架随IntelliJ插件一起提供。 I wouldn't recommend using it since it's not maintained for 1 year. 我不建议使用它,因为它没有维持1年。

Personally i use 1st solution since it's fast, easy and no need to mess with annotations and workarounds. 我个人使用第一个解决方案,因为它快速,简单,无需弄乱注释和变通方法。

You may want to read this article . 您可能想阅读这篇文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM