`writeValue` 和 `writeParcelable` 有什么区别?
What is the difference between `writeValue` and `writeParcelable`?
我一直在寻找一种将对象从一个 Activity 传递到另一个的方法。
不同的教程指出,最好的方法是使 class 可打包。我已经实现了,但还有一个问题。
Office class 中存在对另一个可打包对象 (location
) 的引用。 This tutorial tells to serialize it using dest.writeParcelable(location, flags);
and in.readParcelable(LatLng.class.getClassLoader());
, but the parcelabler 使用 dest.writeValue(location);
创建代码,然后使用 (LatLng) in.readValue(LatLng.class.getClassLoader());
创建代码。
我已经检查过了,它是双向的。
有人可以解释一下这两种方法有什么区别吗?由于某些原因,它们中的任何一个更好吗?谢谢!
public class Office implements Parcelable {
@SuppressWarnings("unused")
public static final Parcelable.Creator<Office> CREATOR = new Parcelable.Creator<Office>() {
@Override
public Office createFromParcel(Parcel in) {
return new Office(in);
}
@Override
public Office[] newArray(int size) {
return new Office[size];
}
};
public final String name;
public final String address;
public final LatLng location;
public Office(String name, String address, LatLng location) {
this.name = name;
this.address = address;
this.location = location;
}
protected Office(Parcel in) {
name = in.readString();
address = in.readString();
// location = (LatLng) in.readValue(LatLng.class.getClassLoader());
location = in.readParcelable(LatLng.class.getClassLoader());
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(address);
// dest.writeValue(location);
dest.writeParcelable(location, flags);
}
}
writeValue
更通用,因为它采用对象作为参数,所以在内部它们检查 instanceOf
对象以调用特定方法。如果您知道类型,我会坚持使用特定类型
我一直在寻找一种将对象从一个 Activity 传递到另一个的方法。 不同的教程指出,最好的方法是使 class 可打包。我已经实现了,但还有一个问题。
Office class 中存在对另一个可打包对象 (location
) 的引用。 This tutorial tells to serialize it using dest.writeParcelable(location, flags);
and in.readParcelable(LatLng.class.getClassLoader());
, but the parcelabler 使用 dest.writeValue(location);
创建代码,然后使用 (LatLng) in.readValue(LatLng.class.getClassLoader());
创建代码。
我已经检查过了,它是双向的。
有人可以解释一下这两种方法有什么区别吗?由于某些原因,它们中的任何一个更好吗?谢谢!
public class Office implements Parcelable {
@SuppressWarnings("unused")
public static final Parcelable.Creator<Office> CREATOR = new Parcelable.Creator<Office>() {
@Override
public Office createFromParcel(Parcel in) {
return new Office(in);
}
@Override
public Office[] newArray(int size) {
return new Office[size];
}
};
public final String name;
public final String address;
public final LatLng location;
public Office(String name, String address, LatLng location) {
this.name = name;
this.address = address;
this.location = location;
}
protected Office(Parcel in) {
name = in.readString();
address = in.readString();
// location = (LatLng) in.readValue(LatLng.class.getClassLoader());
location = in.readParcelable(LatLng.class.getClassLoader());
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(address);
// dest.writeValue(location);
dest.writeParcelable(location, flags);
}
}
writeValue
更通用,因为它采用对象作为参数,所以在内部它们检查 instanceOf
对象以调用特定方法。如果您知道类型,我会坚持使用特定类型