gpt4 book ai didi

Android:Parcelable 和 Serializable 之间的区别?

转载 作者:IT老高 更新时间:2023-10-28 12:50:34 26 4
gpt4 key购买 nike

为什么Android提供2个接口(interface)来序列化对象?可序列化对象是否与 Android Binder 和 AIDL 文件互操作?

最佳答案

在 Android 中,我们不能只将对象传递给 Activity 。为此,对象必须实现 SerializableParcelable 接口(interface)。

可序列化

Serializable 是标准的Java 接口(interface)。您可以只实现 Serializable 接口(interface)并添加覆盖方法。这种方法的问题是使用了反射,而且这是一个缓慢的过程。此方法会创建大量临时对象并导致大量垃圾收集。但是,Serializable接口(interface)更容易实现。

看下面的例子(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):

// 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");

您可以传递 ArrayList 的 Parcelable 对象,如下所示:

// 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");

结论

  1. ParcelableSerializable 接口(interface)快
  2. Parcelable 接口(interface)相比 Serializable 接口(interface)需要更多时间来实现
  3. Serializable接口(interface)更容易实现
  4. Serializable 接口(interface)会创建大量临时对象并导致大量垃圾回收
  5. Parcelable数组可以通过android中的Intent传递

关于Android:Parcelable 和 Serializable 之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3323074/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com