How to maintain state of an imageview while rotating?

140 Views Asked by At

I'm kinda stuck here in the onSaveInstanceState and onRestoreInstanceState, every time i run the emulator, after the image taken from gallery and appear on the screen. When i rotate it, the picture disappears.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == GALLERY_REQUEST_CODE && resultCode == RESULT_OK && data != null){
    imageUri = data.getData();
    imageView.setImageURI(imageUri);
    }
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putParcelable("image", imageUri);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    savedInstanceState.getParcelable("image");
    imageView.setImageURI(imageUri);
}
}

Which part that i got wrong?
is it on the "savedInstanceStae.getParcelable....." ?
i don't know how to save imageView state
Thanks once again for the help

1

There are 1 best solutions below

0
Alif On

You have to set the imageUri variable when the state is restoring.

@Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        imageUri = savedInstanceState.getParcelable("image");
        imageView.setImageURI(imageUri);
    }