Serializable 性能低,序列化是产生较多临时变量,易引起频繁GC;
实现:
public class MyClass implements Serializable{......}
Parcelable 性能高,不能在外界条件变化时保持数据持续性,不能用于磁盘数据存储
public class MyClass implements Parceable{
private int mData;
......
除类基本成员和方法外,重写以下方法:
(1)描述,返回0即可;
public int describecontents(){
return 0;
}
(2)写入pacel
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeint(mData);
}
(3)重写Creator
public static final Parcelable.Creator<MyClass > CREATOR = new Parcelable.Creator<MyClass >() {
@Override
public Person createFromParcel(Parcel source) {
MyClass p = new MyClass ();
p.mData=source.readInt();
return p;
}
@Override
public MyClass [] newArray(int size) {
// TODO Auto-generated method stub
return null;
}
};
}