If you, restarting the Activity, requires recovering large sets of data, re-establishing a network connection, or perform other intensive operations then using the onSaveInstanceState()
could potentially cause your noted symptoms:
- A poor user experience (i.e. "show some flickering")
- Require consumption of a lot of memory
onSaveInstanceState()
callbacks are not designed to carry large objects.
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.
However:
While you can return any object, you should never pass an object that is tied to the Activity, such as a Drawable
, an Adapter
, a View
or any other object that's associated with a Context
. If you do, it will leak all the views and resources of the original activity instance. (Leaking resources means that your application maintains a hold on them and they cannot be garbage-collected, so lots of memory can be lost.)
Source
Unless you are able to pass the Object
(s) smoothly I personally think it is more advantageous to handle the configuration change yourself, meaning not to destroy.
If you have a target API of 13 or higher: You must include screenSize
in your configChanges
. Starting with API 13 the screen size also changes on orientation change and you'll need to account for this. Prior to 13 your Activity would handle this itself.
android:configChanges="orientation|screenSize"