How can I save a bitmap with onRetainNonConfigurationInstance () for screen orientation?

In my class, the camera will be open and the program will display a bitmap after the user takes a picture from the phone’s camera, but at the same time, the user rotates the screen to "landscape", the bitmap will disappear, and oncreate () activity will load again and then the camera will open again.

I did not know whether to save the bitmap with onRetainNonConfigurationInstance () or onSaveInstanceState ().

The question is, how can I save a bitmap (taken from a telephone camera) before the user rotates the phone, so that even if the phone is landscape mode, the same bitmap will be displayed on the screen?

--- EDIT --- Adding to AndroidManifest.xml - a good way to get save state?

+6
source share
1 answer

Save it onSaveInstanceState:

@Override public void onSaveInstanceState(Bundle toSave) { super.onSaveInstanceState(toSave); toSave.putParcelable("bitmap", bitmap); } 

And bring it back. Create:

  @Override public void onCreate(Bundle savedState) { super.onCreate(savedState); if (savedState != null) bitmap = savedState.getParcelable("bitmap"); } 
+9
source

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


All Articles