Android - 多态性和 Parcelable

Android - polymorphism and Parcelable

当 writing/reading 来自 parcel 时,我不知道如何使用多态性。 我知道我需要在基础 class 和所有派生的 classes 中实现 Parcelable(如果 subclasses 有我想写入 parcel 的额外属性). 我不明白的是,我不知道是否有可能 - 如何从 Parcelable 中读取 subclass 即,我如何知道我正在从包裹中读取哪种 subclass . 我可以做一些 hack,比如在 parcel 中写入一些指示器,告诉我要使用什么 class 加载器,但我认为会有一些更优雅的方法,否则多态性就没有太多用处了。

为了说明我的问题,假设我有 class 个这样的:

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..
}

矩形Shape.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..
}

圆形Shape.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..
 }

现在,在其他一些 class 我有这样的东西:

 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..
}

有什么方法可以 "tell" android 在写入 parcel 时,subclass 是什么,而不隐式检查实例类型?

我写了这个小片段并将其加载到主 activity 中的 FrameLayout 中进行检查 - 我得到了肯定的结果(包裹中重建的对象类型为 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];
            }
        };
    }
}

并且在主要 activity 中,在 onCreate(Bundle) 方法中:

getFragmentManager().beginTransaction().replace(R.id.frameContainer, Fragment2.newInstance()).commit();

导致我进行此检查的想法: 因为 android 必须弄清楚 class 从包裹中重新创建对象时要使用什么(哪个CREATOR 来使用),我认为在将其放入包中时必须将此附加信息与指定信息(在 writeToParcel(Parcel, int) 方法中指定)一起存储。为了确定这个 class 我认为它可以使用引用的类型或使用对象本身(例如在其上使用 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) 没有被调用)。

请发表评论,让我知道它是否适合您。