0

The app is supposed to save the view which it draws on orientation change, but it doesn't. Can anyone say where the problem is?

I don't get any error but when I change device orientation the view restarts without saving what I drew.

public DrawingView(Context context, AttributeSet attrs) {
    super(context, attrs);
    conAdd();
}

public DrawingView(Context context) {
    super(context);
    conAdd();

}

private void conAdd() {
    paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.GREEN);
    paint.setStyle(Style.STROKE);
    paint.setStrokeCap(Paint.Cap.ROUND);
    paint.setStrokeJoin(Paint.Join.ROUND);
    paint.setStrokeWidth(width);
    path = new Path();
    setOnTouchListener(this);
    setBackgroundColor(Color.BLACK);

}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_4444);
    Canvas canvasNew = new Canvas();
    canvasNew.setBitmap(bitmap);
    canvas = canvasNew;
    super.onSizeChanged(w, h, oldw, oldh);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawPath(path, paint);
    canvas.drawBitmap(bitmap, 0, 0, paint);
}

public boolean onTouch(View v, MotionEvent event) {
    float x = event.getX();
    float y = event.getY();
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        path.reset();
        path.moveTo(x, y);
        invalidate();
    }
    if (event.getAction() == MotionEvent.ACTION_MOVE) {
        if (Math.abs(x - lastX) > 4 || Math.abs(y - lastY) > 4) {
            path.quadTo(lastX, lastY, (lastX + x) / 2, (lastY + y) / 2);
            invalidate();

        }

    }

    if (event.getAction() == MotionEvent.ACTION_UP) {
        path.lineTo(x, y);

        canvas.drawPath(path, paint);
    }

    lastX = x;
    lastY = y;
    return true;

}


 @Override
    protected Parcelable onSaveInstanceState() {
        Bundle inBox = new Bundle();
        inBox.putParcelable("draw", super.onSaveInstanceState());
        inBox.putParcelable("bitmap", bitmap);
        return super.onSaveInstanceState();

    }



 @Override
    protected void onRestoreInstanceState(Parcelable state) 
    {
         if (state instanceof Bundle) {
              Bundle outBox = (Bundle) state;
              this.bitmap = outBox.getParcelable("bitmap");
              super.onRestoreInstanceState(outBox.getParcelable("draw"));
              return;
         }
         super.onRestoreInstanceState(state);
    }

}

Stefan
  • 5,203
  • 8
  • 27
  • 51
Ilazar1x
  • 59
  • 11
  • 1
    I also think your super.onRestoreInstanceState(state); should be BEFORE the other code – Quintin Balsdon Aug 15 '12 at 09:30
  • 1
    @QuintinBalsdon is correct, and also, where is your `onCreate()` and what not? The function that creates the view? you might be overriding the restored view.. – LuckyMe Jul 14 '13 at 21:55

4 Answers4

0

You need to manage configuration changes at runtime - it also took me a long time to find out where this was:

http://developer.android.com/guide/topics/resources/runtime-changes.html

QUOTED:
The following manifest code declares an activity that handles both the screen orientation change and keyboard availability change:

<activity android:name=".MyActivity"
    android:configChanges="orientation|keyboardHidden"
    android:label="@string/app_name">
Quintin Balsdon
  • 5,484
  • 10
  • 54
  • 95
  • You don't need to override the orientation change functionality in order to persist your data state. – Graeme Aug 15 '12 at 08:48
  • http://stackoverflow.com/questions/2032173/configuration-changed-orientation-change-and-destroying-activities-is-this-t – Quintin Balsdon Aug 15 '12 at 09:09
  • It may be laborious on simple unprofessional apps to have to deal with the lifecycle of Activities being destroyed and recreated - this doesn't mean you should disable it. Loading alternate resources manually for screen sizes / orientations is just one of many examples which becomes a nightmare without this mechanism. – Graeme Aug 15 '12 at 10:13
0

Have you tried using

<activity android:name=".YourActivity" android:configChanges="orientation|screenSize">

?

Notive that while the "orientation" alone could fail to deliver it, adding the "screenSize" option will do the trick!

gpnr
  • 121
  • 5
0

Your functions are incorrect:

@Override
public void onSaveInstanceState(Bundle savedInstanceState){
    // Your code here

    // Call at the end
    super.onSaveInstanceState(savedInstanceState);
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState){
    // Call at the start
    super.onRestoreInstanceState(savedInstanceState);

    // Your code here
}
LuckyMe
  • 3,820
  • 2
  • 27
  • 35
0

You are returning super.onSaveInstanceState(); instead of returning your own bundle (inBox). So nothing is done with your saved data.

Roel
  • 3,089
  • 2
  • 30
  • 34