Serializable 和 Parcelable的区别
1、作用
Serializable的作用是为了保存对象的属性到本地文件、数据库、网络流、rmi以方便数据传输,当然这种传输可以是程序内的也可以是两个程序间的。而Android的Parcelable的设计初衷是因为Serializable效率过慢,为了在程序内不同组件间以及不同Android程序间(AIDL)高效的传输数据而设计,这些数据仅在内存中存在,Parcelable是通过IBinder通信的消息的载体。
从上面的设计上我们就可以看出优劣了。
2、效率及选择
Parcelable的性能比Serializable好,在内存开销方面较小,所以在内存间数据传输时推荐使用Parcelable,如activity间传输数据,而Serializable可将数据持久化方便保存,所以在需要保存或网络传输数据时选择Serializable,因为android不同版本Parcelable可能不同,所以不推荐使用Parcelable进行数据持久化
3、编程实现
对于Serializable,类只需要实现Serializable接口,并提供一个序列化版本id(serialVersionUID)即可。而Parcelable则需要实现writeToParcel、describeContents函数以及静态的CREATOR变量,实际上就是将如何打包和解包的工作自己来定义,而序列化的这些操作完全由底层实现。
4、高级功能上
Serializable序列化不保存静态变量,可以使用Transient关键字对部分字段不进行序列化,也可以覆盖writeObject、readObject方法以实现序列化过 程自定义
1. 文件目录结构
2. Serializable对象:SerializableObject.java 实现接口Serializable
package com.silion.serializableandparcelable;
import java.io.Serializable;
/**
* Created by silion on 2015/10/14.
*/
public class SerializableObject implements Serializable {
private static final long serialVersionUID = 1759681796198207841L;
private String str;
private int i;
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
}
package com.silion.serializableandparcelable;
import android.os.Parcel;
import android.os.Parcelable;
/**
* Created by silion on 2015/10/14.
*/
public class ParcelableObject implements Parcelable {
private String str;
private int i;
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.str);
dest.writeInt(this.i);
}
public ParcelableObject() {
}
protected ParcelableObject(Parcel in) {
this.str = in.readString();
this.i = in.readInt();
}
public static final Creator CREATOR = new Creator() {
public ParcelableObject createFromParcel(Parcel source) {
return new ParcelableObject(source);
}
public ParcelableObject[] newArray(int size) {
return new ParcelableObject[size];
}
};
}
SerializableAndParcelable
Hello world!
Settings
Serializable
Parcelable
package com.silion.serializableandparcelable;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button serializableButton = (Button) findViewById(R.id.serializable);
serializableButton.setOnClickListener(this);
Button parcelableButton = (Button) findViewById(R.id.parcelable);
parcelableButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.serializable: {
serializableMethod();
break;
}
case R.id.parcelable: {
parcelableMethod();
break;
}
default:
break;
}
}
public void parcelableMethod() {
ParcelableObject parcelableObject = new ParcelableObject();
parcelableObject.setStr("silion - parcelable");
parcelableObject.setI(0);
Intent intent = new Intent(this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable("parcelable", parcelableObject);
intent.putExtras(bundle);
startActivity(intent);
}
public void serializableMethod() {
SerializableObject serializableObject = new SerializableObject();
serializableObject.setStr("silion - serializable");
serializableObject.setI(1);
Intent intent = new Intent(this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("serializable", serializableObject);
intent.putExtras(bundle);
startActivity(intent);
}
}
package com.silion.serializableandparcelable;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
* Created by silion on 2015/10/14.
*/
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
LinearLayout layout = (LinearLayout) findViewById(R.id.contentLayout);
TextView serializableTextView = new TextView(this);
TextView parcelableTextView = new TextView(this);
SerializableObject serializableObject = (SerializableObject) getIntent().getSerializableExtra("serializable");
ParcelableObject parcelableObject = getIntent().getParcelableExtra("parcelable");
if (serializableObject != null) {
serializableTextView.setText("str = " + serializableObject.getStr() + ", i = " + serializableObject.getI());
}
if(parcelableObject != null) {
parcelableTextView.setText("str = " + parcelableObject.getStr() + ", i = " + parcelableObject.getI());
}
layout.addView(serializableTextView);
layout.addView(parcelableTextView);
}
}