Android - Why is onSaveInsanceState () used to save a non-displayed bitmap object?

I am making a simple drawing application.

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

I read that if the orientation changes, the action is destroyed and recreated ( onCreate(Bundle created) is called). I am not sure if this means that it should also be called onSavedInstanceState(Bundle bundle) , because in my application it is called only if another action focuses on my main activity, but not when turning to an album / portrait.

I'm just looking for a way to save an existing bitmap and convey it to my main activity when changing orientation. How to do this if my onSaveInstanceState never called?

Also, since Bitmap implements parceable , I have already used it.

Here is 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 would be appreciated.

EDIT:

I deleted this line from the AndroidManifest.xml file:

 android:configChanges="orientation" 

and now onSaveInstanceState() is called when the orientation on the device changes.

+6
source share
4 answers

Save the bitmap in onPause() or onDestroy() , because I think that onSavedInstanceState() never called by the android system when the orientation is switched.

-eleven
source

You should fully read this article .

... you may not be able to completely restore your activity state with the Bundle, which the system saves for you using the onSaveInstanceState() callback - it is not designed to transfer large objects (such as bitmaps) and the data inside it must be serialized then deserialized which can consume a lot of memory and configuration changes are slow. In such a situation, you can lighten the burden of reinitializing your activity by keeping the object stateful when your activity restarts due to a configuration change.

To save an object during runtime configuration changes:

  • Cancel the onRetainNonConfigurationInstance() method to return the object you would like to save.
  • When your activity is created again, call getLastNonConfigurationInstance() to restore your object.
+7
source

I am browsing Android tutorials from TheNewBoston https://thenewboston.com/

In the 4th section, Travis led us through the camera application, which can also set the image as wallpaper. However, the problem with the disappearing bitmap during rotation was not solved, but he proposed only the android:configChanges="orientation" solution android:configChanges="orientation" , which temporarily blocks the screen rotation.

Using the information from this page, I added

 if (savedInstanceState != null) { bmp = savedInstanceState.getParcelable("bitmap"); iv.setImageBitmap(bmp); } 

at the end of onCreate

and added this override

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

in class.

This solved the fading bitmap problem.

+5
source

The onConfigurationChanged () method is likely to be the easiest solution to your problem. It maintains the current state of applications between orientation switches.

There is more information about this in this one.

0
source

Source: https://habr.com/ru/post/919767/


All Articles