Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
404 views
in Technique[技术] by (71.8m points)

android - 如何使用意图将对象从一个Android活动发送到另一个?(How to send an object from one Android Activity to another using Intents?)

如何使用Intent类的putExtra()方法将自定义类型的对象从一个Activity传递到另一个Activity

  ask by community wiki translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you're just passing objects around then Parcelable was designed for this.

(如果您只是传递对象,那么Parcelable就是为此而设计的。)

It requires a little more effort to use than using Java's native serialization, but it's way faster (and I mean way, WAY faster).

(与使用Java的本机序列化相比,它需要付出更多的努力,但是它的速度更快(我的意思是, WAY更快)。)

From the docs, a simple example for how to implement is:

(在文档中,有关如何实现的一个简单示例是:)

// simple class that just has one member property as an example
public class MyParcelable implements Parcelable {
    private int mData;

    /* everything below here is for implementing Parcelable */

    // 99.9% of the time you can just ignore this
    @Override
    public int describeContents() {
        return 0;
    }

    // write your object's data to the passed-in Parcel
    @Override
    public void writeToParcel(Parcel out, int flags) {
        out.writeInt(mData);
    }

    // this is used to regenerate your object. All Parcelables must have a CREATOR that implements these two methods
    public static final Parcelable.Creator<MyParcelable> CREATOR = new Parcelable.Creator<MyParcelable>() {
        public MyParcelable createFromParcel(Parcel in) {
            return new MyParcelable(in);
        }

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

    // example constructor that takes a Parcel and gives you an object populated with it's values
    private MyParcelable(Parcel in) {
        mData = in.readInt();
    }
}

Observe that in the case you have more than one field to retrieve from a given Parcel, you must do this in the same order you put them in (that is, in a FIFO approach).

(请注意,如果要从给定的宗地中检索多个字段,则必须按照它们放入字段的顺序(即,采用FIFO方法)进行操作。)

Once you have your objects implement Parcelable it's just a matter of putting them into your Intents with putExtra() :

(一旦你有你的对象实现Parcelable它只是将它们放在你的事意图putExtra() :)

Intent i = new Intent();
i.putExtra("name_of_extra", myParcelableObject);

Then you can pull them back out with getParcelableExtra() :

(然后,您可以使用getParcelableExtra()将它们拉出:)

Intent i = getIntent();
MyParcelable myParcelableObject = (MyParcelable) i.getParcelableExtra("name_of_extra");

If your Object Class implements Parcelable and Serializable then make sure you do cast to one of the following:

(如果您的对象类实现了Parcelable和Serializable,则请确保将其强制转换为以下之一:)

i.putExtra("parcelable_extra", (Parcelable) myParcelableObject);
i.putExtra("serializable_extra", (Serializable) myParcelableObject);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...