As far as I've understood, your Android activity will be recreated for the new orientation on any orientation changes.
Is there a way to store / save some of the data from the original orientation upon the orientation change?
I'd like to store some Bitmaps, so I don't have to load it again on the orientation change.
you can try using sharedpreferences:
editor edit = preferences.edit(); edit.putString("username", "new_value_for_user"); edit.commit();
Using static variables/classes is a bad approach in terms of maintainability and debugging.
I have been using
Activity.onRetainNonConfigurationInstance
but I found out just now that this is deprecated (probably since honeycomb or later). Activity.onRetainNonConfigurationInstanceUsing this method, just call
Activity.getLastNonConfigurationInstance
to retrieve the same object you returned in theonRetainNonConfigurationInstance
. Be sure to check for null and cast to the right class (you can return/get any class). Activity.getLastNonConfigurationInstanceA sample usage in pseudo-code would be:
So, if you are targetting up to 2.x.x you can use that method. Otherwise, google recommends you to use
Fragment.setRetainInstance
. This is backwards compatible via the compability package.Fragment.setRetainInstance
Save the items in a parent activity or static utility class.
Otherwise, you can use the manifest to tell the app to not destroy the activity on screen resizes. Check out this article: http://developer.android.com/guide/topics/resources/runtime-changes.html
Process running your activity will not be restarted. Android framework will just create a new instance of your activity. So as simplest solution you can store your data in static variables.
Actually there is a very nice article on the Android Developer site which covers this topic.
The full article.