Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 333
Shahar Avatar asked Jul 05 '12 14:07

Shahar


People also ask

What is the purpose of using onSaveInstanceState () here?

As such, use onSaveInstanceState() to store a minimal amount of data necessary, such as an ID, to re-create the data necessary to restore the UI back to its previous state should the other persistence mechanisms fail. Most apps should implement onSaveInstanceState() to handle system-initiated process death.

How do I save a bitmap in SQL?

If you have Bitmap image then you can do following. Bitmap photo = <Your image> ByteArrayOutputStream bos = new ByteArrayOutputStream(); photo. compress(Bitmap. CompressFormat.

Which method is be called to allow the activity to save per instance state?

Save activity instance state in the onSaveInstanceState() method. Instance state data is stored as simple key/value pairs in a Bundle. Use the Bundle methods to put data into and get data back out of the bundle. Restore the instance state in onCreate(), which is the preferred way, or onRestoreInstanceState().

What is Save instance?

The savedInstanceState is a reference to a Bundle object that is passed into the onCreate method of every Android Activity. Activities have the ability, under special circumstances, to restore themselves to a previous state using the data stored in this bundle.


2 Answers

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.
like image 124
Dheeraj Vepakomma Avatar answered Oct 20 '22 19:10

Dheeraj Vepakomma


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.

like image 32
Ranbir Avatar answered Oct 20 '22 19:10

Ranbir