问题描述
为什么Android提供了2个对象序列化接口?Serializable 对象是否与 Android Binder
和 AIDL 文件互操作?
Why does Android provide 2 interfaces for serializing objects? Do Serializable objects interopt with Android Binder
and AIDL files?
推荐答案
在 Android 中,我们不能仅仅将对象传递给活动.为此,对象必须实现 Serializable
或 Parcelable
接口.
In Android we cannot just pass objects to activities. To do this the objects must either implement Serializable
or Parcelable
interface.
可序列化
Serializable
是一个标准的 Java 接口.您可以只实现 Serializable
接口并添加覆盖方法.这种方法的问题在于使用了反射,而且这是一个缓慢的过程.此方法会创建大量临时对象并导致相当多的垃圾收集.但是,Serializable
接口更容易实现.
Serializable
is a standard Java interface. You can just implement Serializable
interface and add override methods. The problem with this approach is that reflection is used and it is a slow process. This method creates a lot of temporary objects and causes quite a bit of garbage collection. However, Serializable
interface is easier to implement.
看下面的例子(可序列化):
Look at the example below (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
过程比 Serializable
快得多.造成这种情况的原因之一是我们明确说明了序列化过程,而不是使用反射来推断它.代码已为此目的进行了大量优化也是理所当然的.
Parcelable
process is much faster than Serializable
. One of the reasons for this is that we are being explicit about the serialization process instead of using reflection to infer it. It also stands to reason that the code has been heavily optimized for this purpose.
看下面的例子(Parcelable):
Look at the example below (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
如下:
You can pass ArrayList
of Parcelable objects as below:
// 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
数组可以通过android中的Intent传递
Parcelable
is faster thanSerializable
interfaceParcelable
interface takes more time to implement compared toSerializable
interfaceSerializable
interface is easier to implementSerializable
interface creates a lot of temporary objects and causes quite a bit of garbage collectionParcelable
array can be passed via Intent in android
这篇关于Android:Parcelable 和 Serializable 之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!