0

I am currently using the code here, albeit heavily modified to suit my needs https://stackoverflow.com/a/28186621/4541217 such as I need to take an image from the camera as well as select from the gallery. I am also zooming the image.

This all works nicely, except for one issue. I lose things when I rotate the device.

I have

    bTemp = null;

    if(getLastNonConfigurationInstance() != null) {
        bTemp = getLastNonConfigurationInstance();

    }

in my onCreate, and an override...

@Override
@Deprecated
public Object onRetainNonConfigurationInstance() {
    return bTemp;
}

I can make this return the image but I lose all of my stroke information.

From the example, I have tried saving the Uri, the alteredBitmap, the bitmap and the choosenImageView. However, none of these are working. If I take a photo, scribble on it, then before doing anything else, using the alteredBitmap, if I rotate, then I get the first set of strokes. However, nothing after that.

Can anyone help me to keep my stroke information on rotate please?

David
  • 214
  • 3
  • 15

2 Answers2

0

Learn about the activity lifecycle.

You need to override functions like onPause, onResume, and use the savedInstanceState.

  • Thank you. I have read all of that, but only partly wiser. It appears that I need to save the image state in onSaveInstanceState, which I can then restore in onCreate when the device is rotated. However, and this is the reason for my question... I do not know what object to save, that will give me my base image, plus all of the scribbles. – David Apr 22 '18 at 21:22
0

I managed to work it out eventually, so for anyone else that is trying to do the same, here is what I did.

Following on from the example link in my opening post, in order to make it stick while rotating...

in the onRetainNonConfigurationInstance, keep the alteredBitmap. (This is in the Activity)

@Override
@Deprecated
public Object onRetainNonConfigurationInstance() {
    return alteredBitmap;
}

then, in the onCreate of the activity...

    if(getLastNonConfigurationInstance() != null) {

        bmp = (Bitmap)getLastNonConfigurationInstance();

        alteredBitmap = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());

        choosenImageView.setNewImage(alteredBitmap, bmp);
    }

notice that the "bmp" is what was sent from alteredBitmap, and now alteredBitmap is the new image. This is then passed into the setNewImage in the DrawableImageView.

David
  • 214
  • 3
  • 15