这一开始写分享, 想写的东西是愈来愈多, 不少以前看过可是比较模糊的都想写(手动doge, 这不, 今天看到个项目用了Parcelable, 就想着看看这俩序列化方式html
序列化不知道你们都用在哪里, 不考虑进程间通讯, 好像只在Intent.putExtra()里用到, 那么为啥有了Serializable还要搞个Parcelable呢? 这俩有啥不同呢?java
这个是标准Java中提供的序列化方法, 优势没别的, 简单好用, 这里的好用, 不是说性能, 而是使用感觉= =. 使用起来很简单, 直接实现Serializable接口便可. 超级无脑android
给个Google Reference里的使用方法, 主要是要提供一个叫CREATOR非空的静态字段而且这个字段实现Parcelable.Creator类, 这里好绕啊, 不过有官方示例:git
public class MyParcelable implements Parcelable {
private int mData;
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel out, int flags) {
out.writeInt(mData);
}
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];
}
};
private MyParcelable(Parcel in) {
mData = in.readInt();
}
}
复制代码
到这里是否是有人以为就能够结束了? Naive! (手动推眼镜github
咳咳, 以上都没啥问题, 可是并不许确, 由于Serializable不单单只提供了自动序列化方式, 因此像Parcelable同样, 能够自行指定序列化的方式:app
private void writeObject(java.io.ObjectOutputStream out) throws IOException;
private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException;
private void readObjectNoData() throws ObjectStreamException;
复制代码
这个是引用的StackOverflow的提问 :ide
Usual Java serialization on an average Android device (if done right *) is about 3.6 times faster than Parcelable for writes and about 1.6 times faster for reads. Also it proves that Java Serialization (if done right) is fast storage mechanism that gives acceptable results even with relatively large object graphs of 11000 objects with 10 fields each. * The sidenote is that usually everybody who blindly states that "Parcelable is mush faster" compares it to default automatic serialization, which uses much reflection inside. This is unfair comparison, because Parcelable uses manual (and very complicated) procedure of writing data to the stream. What is usually not mentioned is that standard Java Serializable according to the docs can also be done in a manual way, using writeObject() and readObject() methods. For more info see JavaDocs. This is how it should be done for the best performance.性能
大概是说Serializable速度很快, 在平均水平的Android机器上比Parcelable读取快1.6倍, 写入快3.6倍...Parcelable比Serializable快, 是由于对比的是自动序列化的Serializable...测试
彻底跟以前的说法反着了啊...this
还有个说法是这样的
Parcelable不能使用在要将数据存储在磁盘上的状况,由于Parcelable不能很好的保证数据的持续性在外界有变化的状况下。尽管Serializable效率低点, 也不提倡用,但在这种状况下,仍是建议你用Serializable 。
来源是 这个连接 11年写的, 已是我找到的最先的了, 不知道缘由是啥, 有懂的老哥能够指教交流一下
借这位大佬的代码一用
So, if serializable is faster and easier to implement, why android has parcelable at all? The reason is native code. Parcelable is created not just for interprocess communication. It also can be used for intercode communication. You can send and recieve objects from C++ native layer. That's it.
What should you choose? Both will work good. But I think that Parcelable is better choice since it is recommended by google and as you can see from this thread is much more appreciated.
因此各位之后仍是看需求使用这俩吧, 至少干掉自动序列化, 一点儿性能都不能少(手动Doge