首页 > 技术文章 > android之Parcelable

zhisuoyu 2016-03-06 23:23 原文

java编程中,为了将对象的状态保存,需要将对象序列化。

在android中,序列化有两种方法可供选择,一个是java自带的序列化方法,只需实现Serializeable接口即可;另一个是android提供的方法,要实现Parcelable接口,并实现其中的一些方法,相对要复杂一些。

两者区别:

Parcelable消耗内存少,更加高效。

Serializeable更适合长期保存数据,和网络传输。

 

下面主要讲解Parceable的使用。

将想要序列化的对象的类实现Parcelable接口,并实现其中的方法:

public class MyParcelable implements Parcelable {
     int mData;

    public MyParcelable(int mData){
        this.mData=mData;
    }

    public int describeContents() {
        return 0;
    }

    
    //将对象打包为Parcel类型对象
    public void writeToParcel(Parcel out, int flags) {
        out.writeInt(mData);
    }

    //将Parcel对象解压为MyParcelable对象
    public static final Creator<MyParcelable> CREATOR
            = new Creator<MyParcelable>() {
        public MyParcelable createFromParcel(Parcel in) {
            return new MyParcelable(in);
        }

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

    //从Parcel对象中读取数据
    private MyParcelable(Parcel in) {
        mData = in.readInt();
    }
}

 

在MainActivity中添加到Intent中,并传递:

        Intent intent = new Intent(MainActivity.this, SecActivity.class);
        Bundle bundle = new Bundle();
        MyParcelable myParcelable = new MyParcelable(99);
        bundle.putParcelable("myParcelable", myParcelable);
        intent.putExtras(bundle);
        startActivity(intent);

  

在SecActivity中获取:

MyParcelable myParcelable=getIntent().getParcelableExtra("myParcelable");

 


 

如果新类型中的成员的数据类型为数组、List、Map或自定义类型时:

public class UserInfo implements Parcelable {

    int age;
    MyType[] hobbies;
    MyType myType;
    List<MyType> list;
    HashMap<String, MyType> map;


    public UserInfo(int age, MyType[] hobbies, MyType myType, List<MyType> list, HashMap<String, MyType> map) {
        this.age = age;
        this.hobbies = hobbies;
        this.myType = myType;
        this.list = list;
        this.map = map;
    }

    protected UserInfo(Parcel in) {
        age = in.readInt();
        hobbies = in.createTypedArray(MyType.CREATOR);
        myType = in.readParcelable(MyType.class.getClassLoader());
        list = in.createTypedArrayList(MyType.CREATOR);
        map=in.readHashMap(HashMap.class.getClassLoader());
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(age);
        dest.writeTypedArray(hobbies, flags);
        dest.writeParcelable(myType, flags);
        dest.writeTypedList(list);
        dest.writeMap(map);
    }

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

    public static final Creator<UserInfo> CREATOR = new Creator<UserInfo>() {
        @Override
        public UserInfo createFromParcel(Parcel in) {
            return new UserInfo(in);
        }

        @Override
        public UserInfo[] newArray(int size) {
            return new UserInfo[size];
        }
    };

    @Override
    public String toString() {
        return "UserInfo{" +
                "age=" + age +
                ", hobbies=" + Arrays.toString(hobbies) +
                ", myType=" + myType +
                ", list=" + list +
                ", map=" + map +
                '}';
    }
}

 

public class MyType implements Parcelable{

    String name;
    int age;

    public MyType(String name, int age) {
        this.name = name;
        this.age = age;
    }

    protected MyType(Parcel in) {
        name = in.readString();
        age = in.readInt();
    }

    public static final Creator<MyType> CREATOR = new Creator<MyType>() {
        @Override
        public MyType createFromParcel(Parcel in) {
            return new MyType(in);
        }

        @Override
        public MyType[] newArray(int size) {
            return new MyType[size];
        }
    };

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeInt(age);
    }

    @Override
    public String toString() {
        return "MyType{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

 

推荐阅读