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

我与影子孤独终老i 提交于 2019-12-23 09:37:22

问题


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.


回答1:


Save your bitmap in onPause() or onDestroy() because i think onSavedInstanceState() is never called by the android sytem when switching orientation.




回答2:


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.



回答3:


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.




回答4:


An onConfigurationChanged() method would probably be the simplest solution to your problem. It saves the apps current state between orientation switches

This link has more information about it.



来源:https://stackoverflow.com/questions/11346275/android-why-is-using-onsaveinsancestate-to-save-a-bitmap-object-not-being-ca

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!