DrawableView icon indicating copy to clipboard operation
DrawableView copied to clipboard

Parcelable protocol requires that the class implements Parcelable

Open woilsy opened this issue 5 years ago • 0 comments

When the onRestoreInstanceState function callbacks, a BadParcelableException is thrown. I tried to solve it in the following way. in DrawableView,

@Override
 protected Parcelable onSaveInstanceState() {
     Parcelable parcelable = super.onSaveInstanceState();
     Bundle bundle = new Bundle();
     bundle.putParcelable("super_data", parcelable);
     DrawableViewSaveState state = new DrawableViewSaveState();
     state.setPaths(paths);
     bundle.putParcelable("path_data", state);
     return bundle;
 }

@Override
 protected void onRestoreInstanceState(Parcelable state) {
     if (state instanceof Bundle) {
         Bundle bundle = (Bundle) state;
         DrawableViewSaveState saveState = bundle.getParcelable("path_data");
         if (saveState != null) {
             paths.addAll(saveState.getPaths());
             for (SerializablePath p : paths) {
                 p.loadPathPointsAsQuadTo();
             }
         }
         Parcelable superData = bundle.getParcelable("super_data");
         super.onRestoreInstanceState(superData);
     } else {
         super.onRestoreInstanceState(state);
     }
 }

new DrawableViewSaveState,

public class DrawableViewSaveState implements Parcelable {

    private ArrayList<SerializablePath> paths;

    public ArrayList<SerializablePath> getPaths() {
        return paths;
    }

    public void setPaths(ArrayList<SerializablePath> paths) {
        this.paths = paths;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeList(this.paths);
    }

    public DrawableViewSaveState() {
    }

    protected DrawableViewSaveState(Parcel in) {
        this.paths = new ArrayList<SerializablePath>();
        in.readList(this.paths, SerializablePath.class.getClassLoader());
    }

    public static final Creator<DrawableViewSaveState> CREATOR = new Creator<DrawableViewSaveState>() {
        @Override
        public DrawableViewSaveState createFromParcel(Parcel source) {
            return new DrawableViewSaveState(source);
        }

        @Override
        public DrawableViewSaveState[] newArray(int size) {
            return new DrawableViewSaveState[size];
        }
    };
}

woilsy avatar Jan 17 '20 02:01 woilsy