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.
As such, use onSaveInstanceState() to store a minimal amount of data necessary, such as an ID, to re-create the data necessary to restore the UI back to its previous state should the other persistence mechanisms fail. Most apps should implement onSaveInstanceState() to handle system-initiated process death.
If you have Bitmap image then you can do following. Bitmap photo = <Your image> ByteArrayOutputStream bos = new ByteArrayOutputStream(); photo. compress(Bitmap. CompressFormat.
Save activity instance state in the onSaveInstanceState() method. Instance state data is stored as simple key/value pairs in a Bundle. Use the Bundle methods to put data into and get data back out of the bundle. Restore the instance state in onCreate(), which is the preferred way, or onRestoreInstanceState().
The savedInstanceState is a reference to a Bundle object that is passed into the onCreate method of every Android Activity. Activities have the ability, under special circumstances, to restore themselves to a previous state using the data stored in this bundle.
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:
- Override the
onRetainNonConfigurationInstance()
method to return the object you would like to retain.- When your activity is created again, call
getLastNonConfigurationInstance()
to recover your object.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With