0

I have similar question to this one. Now my problem is i don't know what to put in here private int stuff; The first answer is giving me this error 'SavedState()' is not public in android.app.Fragment.SavedState'. Cannot be accessed from outside package..

    //no idea what to do here.
    private int stuff;
    private Bitmap customBitmap;

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh)
    {
        customBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        customCanvas = new Canvas(customBitmap);
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        //Draws stuff into the canvas.
        canvas.drawBitmap(customBitmap, 0, 0, linePaintOne);

    }

    @Override
    protected Parcelable onSaveInstanceState()
    {
        Bundle bundle = new Bundle();
        bundle.putParcelable("draw", super.onSaveInstanceState());
        bundle.putParcelable("bitmap", customBitmap);
        bundle.putInt("bitmap", this.stuff);
        System.out.println("onSave");
        //return super.onSaveInstanceState();
        return bundle;
    }

    @Override
    protected void onRestoreInstanceState(Parcelable state) {

        if (state instanceof Bundle)
        {
            Bundle bundle = (Bundle) state;
            customBitmap = bundle.getParcelable("bitmap");
            this.stuff = bundle.getInt("stuff");
            state = bundle.getParcelable("draw");
            System.out.println("onRestore1");
        }
        System.out.println("onRestore2");
        super.onRestoreInstanceState(state);
    }

Things i tried:

how can I save a bitmap with onRetainNonConfigurationInstance() for screen orientation?

^ this one is giving me This view's id is id/view. Make sure other views do not use the same id..

@Override
    protected Parcelable onSaveInstanceState()
    {
        super.onSaveInstanceState();
        Bundle bundle = new Bundle();
        bundle.putParcelable("bitmap", customBitmap);
        return bundle;
    }

    @Override
    protected void onRestoreInstanceState(Parcelable state) {

        super.onRestoreInstanceState(state);
        Bundle bundle = new Bundle();
        customBitmap = bundle.getParcelable("bitmap");
    }

http://it-ride.blogspot.co.nz/2010/08/save-image-in-instance-state-android.html

Community
  • 1
  • 1
Google0593
  • 59
  • 13

1 Answers1

0

You will need to implement your own version of SavedState as an inner class of your custom view. It's a bit verbose as it has to implement Parcelable. It would be something like:

@Override
protected Parcelable onSaveInstanceState() {

    return new SavedState(
            super.onSaveInstanceState(),
            customBitmap,
            stuff
    );
}

@Override
protected void onRestoreInstanceState(Parcelable state) {

    SavedState savedState = (SavedState) state;
    super.onRestoreInstanceState(savedState.getSuperState());

    stuff = savedState.getStuff();
    customBitmap = savedState.getBitmap();
}

protected static class SavedState extends BaseSavedState {

    private final Bitmap bitmap;
    private final int stuff;

    public SavedState(Parcelable superState, Bitmap bitmap, int stuff) {
        super(superState);
        this.bitmap = bitmap;
        this.stuff = stuff;
    }

    public SavedState(Parcel source) {
        super(source);
        this.bitmap = Bitmap.CREATOR.createFromParcel(source);
        this.stuff = source.readInt();
    }

    public Bitmap getBitmap() {return bitmap;}
    public int getStuff() {return stuff;}

    @Override
    public void writeToParcel(Parcel destination, int flags) {

        super.writeToParcel(destination, flags);
        bitmap.writeToParcel(destination, 0);
        destination.writeInt(stuff);
    }

    public static final Parcelable.Creator<SavedState> CREATOR = new Creator<SavedState>() {

        public SavedState createFromParcel(Parcel in) {
            return new SavedState(in);
        }

        public SavedState[] newArray(int size) {
            return new SavedState[size];
        }

    };
}
Jahnold
  • 7,623
  • 2
  • 37
  • 31