PaNaVTEC / DrawableView

A view that allows to paint and saves the result as a bitmap
http://panavtec.me
Apache License 2.0
585 stars 99 forks source link

Parcelable protocol requires that the class implements Parcelable #29

Open woilsy opened 4 years ago

woilsy commented 4 years ago

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];
        }
    };
}