Android - Why is using onSaveInsanceState() to save a bitmap object not being called?
You should read this article completely.
...it might not be possible for you to completely restore your activity state with the Bundle that the system saves for you with the
onSaveInstanceState()
callback—it is not designed to carry large objects (such as bitmaps) and the data within it must be serialized then deserialized, which can consume a lot of memory and make the configuration change slow. In such a situation, you can alleviate the burden of reinitializing your activity by retaining a stateful Object when your activity is restarted due to a configuration change.To retain an object during a runtime configuration change:
- Override the
onRetainNonConfigurationInstance()
method to return the object you would like to retain.- When your activity is created again, call
getLastNonConfigurationInstance()
to recover your object.
I'm going through the Android tutorials from TheNewBoston https://thenewboston.com/
In the 4th section, Travis walked us through making a Camera application which can set a picture as wallpaper as well. The problem with the vanishing bitmap on rotation wasn't resolved however, he only offered the android:configChanges="orientation"
solution, which blocks the screen rotation temporarily.
Using the information from this page, I added
if (savedInstanceState != null) {
bmp = savedInstanceState.getParcelable("bitmap");
iv.setImageBitmap(bmp);
}
at the end of the onCreate
and added this override
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable("bitmap", bmp);
}
in the class.
This solved the problem of the vanishing bitmap.