- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的 Android 应用程序中,我基本上有一个实现 Parcelable
的类,其中一个需要从 Parcel
中读取和写入的字段是一个引用到也实现了 Parcelable
的抽象类。
这是抽象类及其具体实现之一:
public abstract AbstractClass1 implements Parcelable {
}
public class ConcreteClass1 extends AbstractClass1 {
private ConcreteClass1(Parcel in) {
this.setSomeData(in.readInt());
}
public static final Parcelable.Creator<ConcreteClass1> CREATOR =
new Parcelable.Creator<ConcreteClass1>() {
public ConcreteClass1 createFromParcel(Parcel in) {
return new ConcreteClass1 (in);
}
public ConcreteClass1[] newArray(int size) {
return new ConcreteClass1[size];
}
};
@Override
public int describeContents() { return 0; }
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.getSomeData());
}
}
到目前为止,这是我对其他 Parcelable
类的了解,该类需要将对 AbstractClass1
的引用读写到其 Parcel
中。我不确定如何让 CREATOR
工作:
public Class2 implements Parcelable {
private int data = 42;
private AbstractClass1 class1;
public Class2(AbstractClass1 c) { this.class1 = c; }
private Class2(Parcel in) {
this.data = in.readInt();
// this line is bad since it requires us to know about
// what concrete class is being set for class1. This
// class should only need to be aware of AbstractClass1
this.class1 = in.readParcelable(ConcreteClass1.class);
}
public static final Parcelable.Creator<Class2> CREATOR =
new Parcelable.Creator<Class2>() {
public Class2 createFromParcel(Parcel in) {
return new Class2(in);
}
public Class2[] newArray(int size) {
return new Class2[size];
}
};
@Override
public int describeContents() { return 0; }
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.data);
dest.writeInt(this.class1);
}
}
有没有关于如何用 Parcelable
处理这个抽象类问题的最佳实践?我知道 Parcelable
并不意味着要成为序列化的通用工具,但它似乎对很多应用程序来说都是一个重要问题,并且非常有必要获得 Parcelable
s 在用户按下后退按钮然后想要返回应用程序的情况下工作,因为需要保存应用程序的状态。我在 AbstractClass1
上的特殊 CREATOR
也不起作用,因为此类不应该知 Prop 体实现。它应该是某种工厂模式吗?
最佳答案
writeParcelable(Parcelable, int)
方法将适当的非抽象类的名称写入 Parcel
(以及您在 writeToParcel
方法)。
与此相反的是 readParcelable(null)
。 readParcelable
首先读回类的名称,然后在调用 Creator
的 createFromParcel
方法。
readParcelable
的参数不是Class
,而是ClassLoader
。如果您传递 null
,则使用默认的 ClassLoader
。
关于java - 我应该如何将 Parcelable 用于抽象类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32941456/
我有两个 Activity,A 和 B。我试图将对象从 Activity A 发送到 Activity B。在 Activity A 中,我可以看到我的列表包含两个项目,但是当我在 Activity
我想在android中使用Parcelable而不是Serializable,因为它更快,推荐。我的问题是,我好像无法打包Java提供的类LinkedHashMap,对吧? 例如 public cla
我在为我的 Android 应用程序使用 parcelables 时遇到了一些问题。 我有一个名为 Transport 的自定义类,其中包含一堆 RoutePoint - 从文件加载并保存在数组中的实
编辑 清理和重建后,没有生成类 我在尝试使用 Parcels.wrap() 时遇到此错误 Unable to find generated Parcelable class for xyz.xyz.m
我有一个扩展 Parcelable 的类,我们称它为 A 类。 我有另一个扩展 Parcelable 的类,我们称之为 B 类。 A 的成员变量之一是 B 类对象的 ArrayList。 我正在尝试编
当我尝试使用 @Parcelize 注释 enum class 或 object 时,会导致错误 'Parcelable' should是一个类,既作为编辑器提示又作为编译失败。我可以 @Parcel
确实是相当简单的场景,但我在 Google 上找不到任何相关内容,所以这里是: class ContainerClass implements Parcelable { List _items;
我有一个实现Parcelable接口(interface)的类: class A implements Parcelable { } 我有另一个类 B 包含一个 A 对象作为实例变量。在类 B 内的
我如何实现写我的Set >使用通用数据类型到我的包裹? 这是我的代码.. dest.writeList(getArrTRA()); dest.writeList(ge
我有一个 Java Bean 类,在某些字段上用 @Parcel(Parcel.Serialization.BEAN) 和 Gson 的 @SerializedName 注释: 问题.java: @P
有时,我收到此错误,我不知道我的代码出了什么问题帮忙! 任何有帮助的建议代码。 这是我的错误报告: com.example E/UncaughtException: java.lang.Runtime
我正在实现具有另一个 Parcelable insde 的 Parcelable 类。 在 OuterParcelable 类中: @Override public void writeToParce
我想编码和解码一个实现 Parcelable 到/来自字节数组的类。 我很清楚 Parcelable 表示不稳定,因此不适合长期存储实例。但是我有一个用例,我需要序列化一个对象,它不是一个展示停止器如
有没有办法使用Parceler使用 Kotlin 数据类和构造函数进行序列化,而不为每个字段使用 @ParcelProperty 注释? 如果我尝试使用这样的库: @Parcel data class
我正在尝试编写一个可打包的数据对象,以便在我的 android 应用程序中从 activityA 传递到 activityB。 我的对象正在传递所有数据,除了我的可用服务类的 arraylist da
我在上课时遇到问题 Parcelable .问题是,我正在尝试将类(class)中的成员写入包裹,该成员是 ArrayList。目的。 ArrayList是 Serializable ,并且列表中的对
我有一个可打包的团队类 @Parcelize class Team(var name: String, var teamMembers: List, var id: UUID): Parcelable
我有一个包含另一个对象的对象,我想使用 Parcelable 将一个 fragment 发送到另一个 fragment ,例如: Fragment fragment = new Fragment();
Parceler 的自述文件指出它可以与其他基于 POJO 的库一起使用,尤其是 SimpleXML。 是否有任何示例可以证明其用法? 我已经成功地将 Parceler 与 GSON 结合使用: Gs
我是 android 的新手,在思考 Parcelable 界面时遇到了一些麻烦。 我终于找到了这个: https://stackoverflow.com/a/2141166/6647053 上述回答
我是一名优秀的程序员,十分优秀!