一.基本介绍
Parcelable是Android提供的序列化接口 类似Serializable,但是使用比Serializable复杂
二.使用方法
1.创建一个类Student实现Parcelable接口
2.实现接口中describeContents、writeToParcel方法
public class Student implements Parcelable {
private int id;
private int age;
private String name;
protected Student(Parcel in) {
this.id = in.readInt();
this.age = in.readInt();
this.name = in.readString();
}
public static final Creator<Student> CREATOR = new Creator<Student>() {
@Override
public Student createFromParcel(Parcel in) {
return new Student(in);
}
@Override
public Student[] newArray(int size) {
return new Student[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
//写入数据的顺序和Student读取的数据顺序要是一致的
parcel.writeInt(id);
parcel.writeInt(age);
parcel.writeString(name);
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
/**
* 这个方法不是必须的 和service进行跨进程通信 生成AIDL的时候要求有这个方法
* @param desc
*/
public void readFromParcel(Parcel desc) {
this.id = desc.readInt();
this.age = desc.readInt();
this.name = desc.readString();
}
}