Android 通过Intent 传递对象
2011-09-21 14:52
537 查看
If you're just passing objects around then
Parcelable was designed for this. 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).
From the docs, a simple example for how to implement is:
Once you have your objects implement
putExtra():
Then you can pull them back out with
getParcelableExtra():
Parcelable was designed for this. 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).
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 public int describeContents() { return 0; } // write your object's data to the passed-in Parcel 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(); } }
Once you have your objects implement
Parcelableit's just a matter of putting them into yourIntents with
putExtra():
Intent i = new Intent(); i.putExtra("name_of_extra", myParcelableObject);
Then you can pull them back out with
getParcelableExtra():
Intent i = getIntent(); MyParcelable myParcelableObject = (MyParcelable) i.getParcelableExtra("name_of_extra");
相关文章推荐
- Android 开发笔记——通过 Intent 传递类对象
- 在Android中通过Intent使用Bundle传递对象
- 在Android中通过Intent使用Bundle传递对象
- Android 开发笔记——通过 Intent 传递类对象
- 在Android中通过Intent使用Bundle传递对象
- Android 开发笔记——通过 Intent 传递类对象
- 在Android中通过Intent使用Bundle传递对象
- Android 通过 Intent 传递类对象
- Android中通过Intent传递对象、集合问题
- Android--通过Intent使用Bundle如何传递对象
- Android ListView 点击item 监听事件,通过intent传递对象
- Android 开发笔记——通过 Intent 传递类对象
- Android 开发笔记——通过 Intent 传递类对象
- Android 开发笔记——通过 Intent 传递类对象
- 在Android中通过Intent使用Bundle传递对象
- android 通过Intent使用Bundle传递对象
- Android 如何通过Intent在activity之间传递自定义对象 推荐
- Android 开发笔记——通过 Intent 传递类对象
- Android 通过 Intent 传递类对象或list对象
- 在Android中通过Intent使用Bundle传递对象