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.
You should read this article completely.