Write a sub class of Parcelable to another Parcel

前端 未结 3 2093
盖世英雄少女心
盖世英雄少女心 2020-12-23 09:15

I have a class that implements Parcelable interface:

class A implements Parcelable {

}

I have another class B th

3条回答
  •  悲哀的现实
    2020-12-23 09:25

    A better way to handle this that avoids reflexion would be to simply call the static creator in the target type like this:

    this.amazingObject = AmazingObject.CREATOR.createFromParcel(in);
    

    and write it to the Parcelable using this.amazingObject.writeToParcel(Parcel, int)

    Internally, when calling readParcelable(ClassLoader) this happens:

    public final  T readParcelable(ClassLoader loader) {
        Parcelable.Creator creator = readParcelableCreator(loader);
        if (creator == null) {
            return null;
        }
        if (creator instanceof Parcelable.ClassLoaderCreator) {
            return ((Parcelable.ClassLoaderCreator)creator).createFromParcel(this, loader);
        }
        return creator.createFromParcel(this);
    }
    

    As you can see the last call of that method is just calling the right Creator but inside readParcelableCreator there is a whole lot of reflexion that we can avoid by just calling it directly.

    This utility calls may be useful:

    import android.os.Parcel;
    import android.os.Parcelable;
    
    public class Parcels {
    
        public static void writeBoolean(Parcel dest, Boolean value) {
            dest.writeByte((byte) (value != null && value ? 1 : 0));
        }
    
        public static Boolean readBoolean(Parcel in){
            return in.readByte() != 0;
        }
    
        public static void writeParcelable(Parcel dest, int flags, Parcelable parcelable) {
            writeBoolean(dest, parcelable == null);
            if (parcelable != null) {
                parcelable.writeToParcel(dest, flags);
            }
        }
    
        public static  T readParcelable(Parcel in, Parcelable.Creator creator) {
            boolean isNull = readBoolean(in);
            return isNull ? null : creator.createFromParcel(in);
        }
    
    }
    

提交回复
热议问题