在Android中,我们不能仅仅将对象传递给活动。要做到这一点,对象必须实现Serializable或Parcelable接口。
系列化
Serializable是一个标准的Java接口。你可以直接实现Serializable接口和添加重写方法。这种方法的问题是使用反射,这是一个缓慢的过程。此方法创建了许多临时对象,并导致相当多的垃圾收集。然而,Serializable接口更容易实现。
请看下面的示例(可序列化):// MyObjects Serializable classimport java.io.Serializable;import java.util.ArrayList;import java.util.TreeMap;import android.os.Parcel;import android.os.Parcelable;public class MyObjects implements Serializable {
private String name;
private int age;
public ArrayList address;
public MyObjects(String name, int age, ArrayList address) {
super();
this.name = name;
this.age = age;
this.address = address;
}
public ArrayList getAddress() {
if (!(address == null))
return address;
else
return new ArrayList();
}
public String getName() {
return name;
}
public String getAge() {
return age;
}}// MyObjects instanceMyObjects mObjects = new MyObjects("name", "age", "Address array here");// Passing MyObjects instance via intentIntent mIntent = new Intent(FromActivity.this, ToActivity.class);mIntent.putExtra("UniqueKey", mObjects);startActivity(mIntent);// Getting MyObjects instanceIntent mIntent = getIntent();MyObjects workorder = (MyObjects) mIntent.getSerializableExtra("UniqueKey");
Parcelable
Parcelable过程比Serializable..造成这种情况的原因之一是,我们明确了序列化过程,而不是使用反射推断它。这也是合理的,这段代码已经为此目的进行了大量的优化。
请看下面的示例(Parcelable):// MyObjects Parcelable classimport java.util.ArrayList;import android.os.Parcel;import android.os.Parcelable;public class MyObjects implements Parcelable {
private int age;
private String name;
private ArrayList address;
public MyObjects(String name, int age, ArrayList address) {
this.name = name;
this.age = age;
this.address = address;
}
public MyObjects(Parcel source) {
age = source.readInt();
name = source.readString();
address = source.createStringArrayList();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(age);
dest.writeString(name);
dest.writeStringList(address);
}
public int getAge() {
return age;
}
public String getName() {
return name;
}
public ArrayList getAddress() {
if (!(address == null))
return address;
else
return new ArrayList();
}
public static final Creator CREATOR = new Creator() {
@Override
public MyObjects[] newArray(int size) {
return new MyObjects[size];
}
@Override
public MyObjects createFromParcel(Parcel source) {
return new MyObjects(source);
}
};}// MyObjects instanceMyObjects mObjects = new MyObjects("name", "age", "Address array here");// Passing MyOjects instanceIntent mIntent = new Intent(FromActivity.this, ToActivity.class);mIntent.putExtra("UniqueKey", mObjects);startActivity(mIntent);// Getting MyObjects instanceIntent mIntent = getIntent();MyObjects workorder = (MyObjects) mIntent.getParcelableExtra("UniqueKey");
你可以通过ArrayList可分割物体如下:// Array of MyObjectsArrayList mUsers;// Passing MyOjects instanceIntent mIntent = new Intent(FromActivity.this, ToActivity.class);mIntent.putParcelableArrayListExtra("UniqueKey", mUsers);startActivity(mIntent);// Getting MyObjects instanceIntent mIntent = getIntent();ArrayList mUsers = mIntent.getParcelableArrayList("UniqueKey");
结语Parcelable比
Serializable界面
Parcelable接口需要更多的时间来实现。
Serializable界面
Serializable接口更容易实现。
Serializable接口创建了许多临时对象,并导致相当多的垃圾收集。
Parcelable数组可以在android中通过意图传递。