Serializable
或Parcelable
接口。
Serializable
Serializable
是一个标准的Java接口。您可以实现Serializable
接口并添加覆盖方法。这种方法的问题在于它使用反射,并且速度很慢。此方法创建大量临时对象并导致相当多的垃圾回收。但是,Serializable
接口更容易实现。// MyObjects Serializable class
import 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<String> address;
public MyObjects(String name, int age, ArrayList<String> address) {
super();
this.name = name;
this.age = age;
this.address = address;
}
public ArrayList<String> getAddress() {
if (!(address == null))
return address;
else
return new ArrayList<String>();
}
public String getName() {
return name;
}
// return age
public int getAge() {
return age;
}
}
// MyObjects instance
MyObjects mObjects = new MyObjects("name", "age", "Address array here");
// Passing MyObjects instance via intent
Intent mIntent = new Intent(FromActivity.this, ToActivity.class);
mIntent.putExtra("UniqueKey", mObjects);
startActivity(mIntent);
// Getting MyObjects instance
Intent mIntent = getIntent();
MyObjects workorder = (MyObjects) mIntent.getSerializableExtra("UniqueKey");
Parcelable
Parcelable
过程比 Serializable
更快。其中一个原因是我们明确了序列化过程,而不是使用反射来推断它。此外,代码已经针对此目的进行了大量优化。
请查看以下示例 (Parcelable):
// MyObjects Parcelable class
import java.util.ArrayList;
import android.os.Parcel;
import android.os.Parcelable;
public class MyObjects implements Parcelable {
private int age;
private String name;
private ArrayList<String> address;
public MyObjects(String name, int age, ArrayList<String> 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<String> getAddress() {
if (!(address == null))
return address;
else
return new ArrayList<String>();
}
public static final Creator<MyObjects> CREATOR = new Creator<MyObjects>() {
@Override
public MyObjects[] newArray(int size) {
return new MyObjects[size];
}
@Override
public MyObjects createFromParcel(Parcel source) {
return new MyObjects(source);
}
};
}
// MyObjects instance
MyObjects mObjects = new MyObjects("name", "age", "Address array here");
// Passing MyOjects instance
Intent mIntent = new Intent(FromActivity.this, ToActivity.class);
mIntent.putExtra("UniqueKey", mObjects);
startActivity(mIntent);
// Getting MyObjects instance
Intent mIntent = getIntent();
MyObjects workorder = (MyObjects) mIntent.getParcelableExtra("UniqueKey");
Parcelable
对象的 ArrayList
:// Array of MyObjects
ArrayList<MyObjects> mUsers;
// Passing MyOjects instance
Intent mIntent = new Intent(FromActivity.this, ToActivity.class);
mIntent.putParcelableArrayListExtra("UniqueKey", mUsers);
startActivity(mIntent);
// Getting MyObjects instance
Intent mIntent = getIntent();
ArrayList<MyObjects> mUsers = mIntent.getParcelableArrayList("UniqueKey");
结论
Parcelable
接口比 Serializable
接口更快。Parcelable
接口相比于 Serializable
接口需要更多的时间来实现。Serializable
接口比较容易实现。Serializable
接口会创建大量临时对象,并引起很多垃圾回收。Parcelable
数组可以通过 Intent 在 Android 中传递。可序列化(Serializable)是Java标准接口。实现该接口可以将类标记为可序列化,Java会在某些情况下自动对其进行序列化。
可被包裹(Parcelable)是一个Android特有的接口,需要你手动实现序列化。它被创建出来是为了比Serializable更高效,并解决默认Java序列化方案的一些问题。
我相信Binder和AIDL使用的是Parcelable对象。
然而,在Intent中也可以使用Serializable对象。
Parcelable是数据传输的推荐方法。但是,如果您像这个存储库中展示的那样正确使用Serializable,您会发现Serializable有时甚至比Parcelable更快。或者至少计时是可比较的。
否,如果序列化得当。
通常情况下,在平均Android设备上(如果*正确完成),Java序列化写入约比Parcelable快3.6倍,读取约比Parcelable快1.6倍。此外,它证明了Java序列化(如果做得好)是一种快速存储机制,即使对于具有每个对象10个字段的相对大的对象图形11000个对象也可以给出可接受的结果。
* 顺便提一下,通常每个盲目地声称“Parcelable更快”的人将其与默认自动序列化进行比较,后者在内部使用了反射。这是不公平的比较,因为Parcelable使用手动(而且非常复杂)的过程将数据写入流中。通常没有提到的是,根据文档,标准的Java Serializable也可以使用writeObject()和readObject()方法手动完成。有关更多信息,请参见JavaDocs。这是为获得最佳性能而应该完成的方式。
原因是本地代码。 Parcelable不仅适用于进程间通信,还可用于交互式编码。您可以从C++本地层发送和接收对象。就是这样。
该选择哪一个?两者都可以很好地工作。但我认为Parcelable是更好的选择,因为它被Google推荐,并且正如您从这个主题中看到的那样,更受欢迎。
实际上,我将成为唯一支持 Serializable 的人。由于设备比几年前好得多,并且还存在其他更微妙的差异,速度差异不再那么显著。有关更多信息,请参见我在博客中发布的内容。
@see http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html
@see http://developer.android.com/reference/android/os/Parcelable.html
请注意,Serializable是标准的Java接口,而Parcelable是用于Android开发的。
在序列化和反序列化方面存在一些性能问题,Parcelable比Serializable快两倍。
请查看以下链接:
http://www.3pillarglobal.com/insights/parcelable-vs-java-serialization-in-android-app-development
如果您在Android Studio中使用Parcelable插件,实现Parcelable可以更快。请搜索“Android Parcelable代码生成器”。
Serializable接口可以像Parcelable一样使用,这样可以获得(不多)更好的性能表现。只需覆盖这两个方法来处理手动编组和解组过程:
private void writeObject(java.io.ObjectOutputStream out)
throws IOException
private void readObject(java.io.ObjectInputStream in)
throws IOException, ClassNotFoundException
然而,我认为在开发本地Android应用时,使用Android API 是一个好方法。
参见: