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.
source share