在从包裹中书写/阅读时,我无法弄清楚如何使用多态性.
我知道我需要在基类中实现Parcelable,并且还需要在所有派生类中实现(在子类具有我想要写入parcel的附加属性的情况下).
我不明白和我不知道甚至可能的是 – 如何从Parcelable读取子类,即,我怎么知道我从包裹中读取什么类的子类.
我可以做一些黑客,比如在包裹中写一些指示器,告诉我要使用什么类加载器,但我认为会有一些更优雅的方式,否则没有多少使用多态.
为了说明我的问题,让我说我有这样的课程:
Shape.class
public class Shape implements Parcelable {
public float area;
public Shape() {
}
public Shape(Parcel in) {
area = in.readFloat();
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeFloat(area);
}
//CREATOR etc..
}
RectangleShape.class
public class RectangleShape extends Shape {
float a;
float b;
public RectangleShape(Parcel in) {
super(in);
a = in.readFloat();
b = in.readFloat();
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeFloat(area);
dest.writeFloat(a);
dest.writeFloat(b);
}
//CREATOR etc..
}
CircleShape.class
public static class CircleShape extends Shape {
float r;
public CircleShape(Parcel in) {
super(in);
r = in.readFloat();
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeFloat(area);
dest.writeFloat(r);
}
//CREATOR etc..
}
现在,在其他一些课程中我有这样的事情:
public class Geometry implements Parcelable {
Shape myShape;
public Geometry (boolean condition) {
if (condition)
myShape = new CircleShape(4.5);
else
myShape = new RectangleShape(3.4, 3.5);
}
@Override
public Geometry(Parcel in) {
in.readParcelable(??? - **how do I know what class loader to put here?** )
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeParcelable(myShape,flags);
}
//CREATOR etc..
}
有没有什么方法可以“告诉”android在写入parcel时它是什么子类,而没有隐式检查实例类型?
最佳答案 我编写了这个小片段并将其加载到主活动中的FrameLayout中进行检查 – 我得到了积极的结果(来自parcel的重建对象是DerivedClass类型,尽管对象的引用都是ObjectClass类型).
import android.app.Fragment;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class Fragment2 extends Fragment {
public ObjectClass obj;
public static final String OBJECT_KEY = "Object key";
public static Fragment2 newInstance(){
Fragment2 fragment = new Fragment2();
Bundle args = new Bundle();
ObjectClass obj = new DerivedClass(); //a DerivedClass object
//referenced by an ObjectClass reference
args.putParcelable(OBJECT_KEY, obj);
fragment.setArguments(args);
return fragment;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main2, container, false);
if (savedInstanceState == null){
Bundle args = getArguments();
obj = args.getParcelable(OBJECT_KEY); //without supplying the ClassLoader
((TextView)view.findViewById(R.id.section_label)).setText(obj.getTitle());
//The text that is displayed is: "Child class"!
}
return view;
}
//The parent class
public static class ObjectClass implements Parcelable {
String title = "Parent class";
public String getTitle(){
return title;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
}
public static final Creator<ObjectClass> CREATOR = new Creator<ObjectClass>() {
@Override
public ObjectClass createFromParcel(Parcel source) {
return new ObjectClass();
}
@Override
public ObjectClass[] newArray(int size) {
return new ObjectClass[size];
}
};
}
//The child class
public static class DerivedClass extends ObjectClass {
String title2 = "Child class";
public String getTitle() {
return title2;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
}
public static final Parcelable.Creator<DerivedClass> CREATOR = new Parcelable.Creator<DerivedClass>() {
@Override
public DerivedClass createFromParcel(Parcel source) {
return new DerivedClass();
}
@Override
public DerivedClass[] newArray(int size) {
return new DerivedClass[size];
}
};
}
}
在主要活动中,在onCreate(Bundle)方法中:
getFragmentManager().beginTransaction().replace(R.id.frameContainer, Fragment2.newInstance()).commit();
我的想法让我进行了这项检查:由于android必须弄清楚从包裹中重新创建对象时要使用的类(使用CREATOR),我认为它必须将这些附加信息与指定信息一起存储(在writeToParcel中指定) (Parcel,int)方法)将它放入束中.为了确定这个类,我认为它可以使用引用的类型或使用对象本身(例如使用getClass()).如果它使用对象类型本身,那当然意味着多态性是可能的.从上面的检查看起来确实如此.
结论并猜测你的例子:
我想你的例子,尽量不要显式传递ClassLoader.传递null.从Parcel#readParcelable(ClassLoader)参数的文档中:
ClassLoader: A ClassLoader from which to instantiate the Parcelable object, or null for the default class loader.
我没有尝试使用你的配置,所以我不确定它是否会像我的例子一样工作.具体来说,我不确定Parcel#readParcelable(ClassLoader = null)是否实际上与Bundle#getParcelable(String)的工作方式相同.但如果我的理由是正确的 – 它应该(我假设Bundle#getParcelable(String)使用默认的ClassLoader,只要没有调用Bundle #setClassLoader(ClassLoader)).
请评论并告诉我它是否适合您.