Problem unmarshalling parcelables

后端 未结 10 1957
旧时难觅i
旧时难觅i 2020-11-27 15:45

I\'ve got a few classes that implement Parcelable and some of these classes contain each other as properties. I\'m marshalling the classes into a Parcel to pass them between

10条回答
  •  悲&欢浪女
    2020-11-27 16:17

    Instead of using writeParcelable and readParcelable use writeToParcel and createFromParcel directly. So the better code is:

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        location.writeToParcel(dest, flags);
        dest.writeLong(start);
        dest.writeLong(end  );
    }
    
    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
        public LayoverType createFromParcel(Parcel in) {
            return new LayoverType(in);
        }
    
        public LayoverType[] newArray(int size) {
            return new LayoverType[size];
        }
    };
    
    private LayoverType(Parcel dest) {
        location = LocationType.CREATOR.createFromParcel(dest);
        start = dest.readLong();
        end   = dest.readLong();
    }
    

提交回复
热议问题