Android中的可序列化和可打包
我不清楚Android中接口Android中的可序列化和可打包,android,Android,我不清楚Android中接口Serializable和Parcelable的不同用法。如果我们更喜欢使用Parcelable,那么为什么我们不使用Serializable。此外,如果我通过任何Web服务传递数据,是否可以Parcelablehelp?如果是的话,那怎么办呢?那么主要的区别是速度,这对手持设备很重要。应该没有你的桌面那么强大 使用适用于goold old Java的Serialisable,您的代码将如下所示: class MyPojo implements Serializabl
Serializable
和Parcelable
的不同用法。如果我们更喜欢使用Parcelable
,那么为什么我们不使用Serializable
。此外,如果我通过任何Web服务传递数据,是否可以Parcelable
help?如果是的话,那怎么办呢?那么主要的区别是速度,这对手持设备很重要。应该没有你的桌面那么强大
使用适用于goold old Java的Serialisable
,您的代码将如下所示:
class MyPojo implements Serializable {
String name;
int age;
}
class MyPojo implements Parcelable {
String name;
int age;
MyPojo(Parcel in) {
name = in.readString();
age = in.readInt();
}
void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(age);
}
int describeContents() {
return 0;
}
// and here goes CREATOR as well and what not
};
不需要额外的方法,因为反射将用于获取所有字段及其值。这可以让我慢下来或者比
要使用Parcelable
,您必须这样写:
class MyPojo implements Serializable {
String name;
int age;
}
class MyPojo implements Parcelable {
String name;
int age;
MyPojo(Parcel in) {
name = in.readString();
age = in.readInt();
}
void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(age);
}
int describeContents() {
return 0;
}
// and here goes CREATOR as well and what not
};
根据guys@google的说法,它可以快得多,也就是说,Parcelable
s
当然,有一些工具可以帮助您添加所有必要的字段,以使类可打包
,例如可能重复检查此项