为无法实现 Parcelable 的对象读取或写入 from/to Parcel
Read or Write from/to Parcel for an object which can not implement Parcelable
我有一个 class 类似实现 Parcelable 的 ObjectA。
public class ObjectA implements Parcelable {
private ObjectB objB;
private int mId;
}
ObjectB 来自添加到项目的 .jar 文件,我无法为该文件实现 Parcelable 接口 class。
private ObjectA(Parcel in) {
}
@Override
public void writeToParcel(Parcel dest, int flags) {
}
所以我想知道是否有任何解决方案可以从 Parcel 读取或写入 Parcel for ObjectB?
当然dest.writeParcelable
和in.readParcelable
是行不通的,因为ObjectB不能实现Parcelable接口。
应该可以通过使用 ObjectB 的处理对象来实现:
public class ObjectBHandler implements Parcelable {
private ObjectB b;
public ObjectBHandler(ObjectB b) {
this.b = b;
}
public ObjectB getB() {
return b;
}
public void setB(ObjectB b) {
this.b = b;
}
/**
* Describe the kinds of special objects contained in this Parcelable's
* marshalled representation.
*
* @return a bitmask indicating the set of special object types marshalled
* by the Parcelable.
*/
@Override
public int describeContents() {
return 0;
}
/**
* Flatten this object in to a Parcel.
*
* @param dest The Parcel in which the object should be written.
* @param flags Additional flags about how the object should be written.
* May be 0 or {@link #PARCELABLE_WRITE_RETURN_VALUE}.
*/
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeDouble(b.getValue());
//...
}
}
如果你的意思是你不能实现接口,因为它在 jar 中,那么可能有一个解决方案:
如果您足够了解 ObjectB 的实现并且知道应该恢复的每个字段,并且如果 ObjectB 不是最终的,我想您仍然可以这样做:
public class ParcelableObjectB extends ObjectB implements Parcelable {...}
您可以通过 getter 从 ObjectB
获取属性并将其写入 Parcel
,当您从 Parcel
读取时 - 您可以创建新的 ObjectB
并设置它由二传手。如果您没有访问权限和任何 setters/getter 您可以使用反射。
我有一个 class 类似实现 Parcelable 的 ObjectA。
public class ObjectA implements Parcelable {
private ObjectB objB;
private int mId;
}
ObjectB 来自添加到项目的 .jar 文件,我无法为该文件实现 Parcelable 接口 class。
private ObjectA(Parcel in) {
}
@Override
public void writeToParcel(Parcel dest, int flags) {
}
所以我想知道是否有任何解决方案可以从 Parcel 读取或写入 Parcel for ObjectB?
当然dest.writeParcelable
和in.readParcelable
是行不通的,因为ObjectB不能实现Parcelable接口。
应该可以通过使用 ObjectB 的处理对象来实现:
public class ObjectBHandler implements Parcelable {
private ObjectB b;
public ObjectBHandler(ObjectB b) {
this.b = b;
}
public ObjectB getB() {
return b;
}
public void setB(ObjectB b) {
this.b = b;
}
/**
* Describe the kinds of special objects contained in this Parcelable's
* marshalled representation.
*
* @return a bitmask indicating the set of special object types marshalled
* by the Parcelable.
*/
@Override
public int describeContents() {
return 0;
}
/**
* Flatten this object in to a Parcel.
*
* @param dest The Parcel in which the object should be written.
* @param flags Additional flags about how the object should be written.
* May be 0 or {@link #PARCELABLE_WRITE_RETURN_VALUE}.
*/
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeDouble(b.getValue());
//...
}
}
如果你的意思是你不能实现接口,因为它在 jar 中,那么可能有一个解决方案: 如果您足够了解 ObjectB 的实现并且知道应该恢复的每个字段,并且如果 ObjectB 不是最终的,我想您仍然可以这样做:
public class ParcelableObjectB extends ObjectB implements Parcelable {...}
您可以通过 getter 从 ObjectB
获取属性并将其写入 Parcel
,当您从 Parcel
读取时 - 您可以创建新的 ObjectB
并设置它由二传手。如果您没有访问权限和任何 setters/getter 您可以使用反射。