Android - Why is using onSaveInsanceState() to save a bitmap object not being called?

Shahar picture Shahar · Jul 5, 2012 · Viewed 9.7k times · Source

I'm making a simple drawing application.

I want to be able to save the user's drawing on the screen when the device orientation changes. This only happens in the main activity.

I read that if the orientation changes, then the activity is destroyed and recreated again (onCreate(Bundle created) being called). I'm not sure if it means that it should also call onSavedInstanceState(Bundle bundle), because in my app it is only called if another activity takes the focus on top of my main activity, but not when rotating to landscape/portrait.

I'm simply looking for a way to save an existing bitmap and pass it to the main activity when the orientation changes. How can I do it if my onSaveInstanceState never gets called?

Also, since Bitmap implements parceable already I used it.

Here's the code from the main activity:

 public void onCreate(Bundle savedInstanceState) {      
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
// some more activity code...

        if (savedInstanceState != null) {
        bitmap = savedInstanceState.getParcelable("bitmap");
        Log.d("STATE-RESTORE", "bitmap created");
        paintBoard.setBitmapBackground(bitmap, false);
        Log.d("RESTORING...", "onRestoreInstanceState()");
    } else {
        Log.d("SavedInstanceState", "null");
    }
}


// Never called when I change orientation on my device
@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    bitmap = Bitmap.createBitmap(paintBoard.getBitmap());
    outState.putParcelable("bitmap", bitmap);
    Log.d("STATE-SAVE", "onSaveInstanceState()");
}

Any help will be appreciated.

EDIT :

I removed this line from the AndroidManifest.xml file:

android:configChanges="orientation"

and now onSaveInstanceState() does get called when I change orientation on the device.

Answer

Dheeraj Vepakomma picture Dheeraj Vepakomma · Jul 5, 2012

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:

  1. Override the onRetainNonConfigurationInstance() method to return the object you would like to retain.
  2. When your activity is created again, call getLastNonConfigurationInstance() to recover your object.