原文网址:http://blog.csdn.net/lixiang0522/article/details/8642202
android中的组件间传递的对象一般实现Parcelable接口,当然也可以使用java的Serializable接口,前者是android专门设计的,效率更高,下面我们就来实现一个Parcelabel。
1. 创建一个类实现Parcelable接口,具体实现如下:
- package com.hebaijun.testparcelable;
- import android.os.Parcel;
- import android.os.Parcelable;
- public class ParcelableData implements Parcelable{
- private String name;
- private int age;
- public ParcelableData(){
- name = "guest";
- age = 20;
- }
- public ParcelableData(Parcel in){
- //顺序要和writeToParcel写的顺序一样
- name = in.readString();
- age = in.readInt();
- }
- public String getName(){
- return name;
- }
- public void setName(String name){
- this.name = name;
- }
- public int getAge(){
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- @Override
- public int describeContents() {
- // TODO Auto-generated method stub
- return 0;
- }
- @Override
- public void writeToParcel(Parcel dest, int flags) {
- // TODO Auto-generated method stub
- dest.writeString(name);
- dest.writeInt(age);
- }
- public static final Parcelable.Creator<ParcelableData> CREATOR = new Parcelable.Creator<ParcelableData>() {
- public ParcelableData createFromParcel(Parcel in) {
- return new ParcelableData(in);
- }
- public ParcelableData[] newArray(int size) {
- return new ParcelableData[size];
- }
- };
- }
2. 通过下面的方法发送对象。Bundle类也实现了Parcelable接口,一般在android中我们是通过Bundle来封装数据并进行传送的。
- Intent intent = new Intent();
- intent.setClass(this, SubActivity.class);
- // 直接添加
- //intent.putExtra("MyData", new ParcelableData());
- // 通过Bundle
- Bundle bundle = new Bundle();
- bundle.putString("MyString", "test bundle");
- bundle.putParcelable("MyData", new ParcelableData());
- intent.putExtras(bundle);
- startActivity(intent);
3. 下面的接收对象的方法。
- //ParcelableData parcelableData = getIntent().getParcelableExtra("MyData");
- Bundle bundle = getIntent().getExtras();
- ParcelableData parcelableData = bundle.getParcelable("MyData");
- String testBundleString = bundle.getString("MyString");
- Log.v("string=", testBundleString);
- Log.v("name=", parcelableData.getName());
- Log.v("age=", ""+parcelableData.getAge());
传输的对象需要实现序列化:有两种方式,一种是实现Serializable接口,就是原来的java方式;另外一种是android的Parcelable方式,这个性能可能好一些,我猜的,但是这在需要手动去写Parcelable接口的实现。
Serializable存数据:
- Person mPerson = new Person();
- mPerson.setName("frankie");
- mPerson.setAge(25);
- Intent mIntent = new Intent(this,ObjectTranDemo1.class);
- Bundle mBundle = new Bundle();
- mBundle.putSerializable(SER_KEY,mPerson);
- mIntent.putExtras(mBundle);
Serializable取数据:
// 获取启动该ResultActivity的Intent |
24 |
Intent intent = getIntent(); |
25 |
// 获取该Intent所携带的数据 |
26 |
Bundle bundle = intent.getExtras(); |
27 |
// 从bundle数据包中取出数据 |
28 |
Person person = (Person) bundle.getSerializable( "person" ); |
Parcelable存数据:
- Intent mIntent = new Intent(this,ObjectTranDemo2.class);
- Bundle mBundle = new Bundle();
- mBundle.putParcelable(PAR_KEY, mBook);
- mIntent.putExtras(mBundle);
Parcelable取数据:
Book mBook = (Book)getIntent().getParcelableExtra(ObjectTranDemo.PAR_KEY);
参考1:http://blog.csdn.net/Android_Tutor/article/details/5740845