I'm interested to know which methods are overridden, when an Android device is rotated (i.e. when configuration changes)?
onSaveInstanceState(...)
, onConfigurationChanged(...)
, onRestoreInstanceState(...)
- something similar to this?
It will be also interesting for me to listen about the whole process connected with changing a configuration. Thanks.
According to the android developer reference you have to use:
For more information see: http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange
You could be interested in seeing: http://developer.android.com/guide/topics/manifest/activity-element.html#config
Here you can find all configuration listener you can listen.
Remember to put them in the android manifest:
Hope this helps...
Go to Manifest and add: android:configChanges="orientation|screenSize"
In Main Activity: Copy and Paste this code:
All Done -
From android developers...
To retain an Object during a runtime configuration change:
onRetainNonConfigurationInstance()
method to return the Object you would like to retain.getLastNonConfigurationInstance()
to recover your Object.Android calls onRetainNonConfigurationInstance() between onStop() and onDestroy() when it shuts down your Activity due to a configuration change. In your implementation of onRetainNonConfigurationInstance(), you can return any Object that you need in order to efficiently restore your state after the configuration change.
A scenario in which this can be valuable is if your application loads a lot of data from the web. If the user changes the orientation of the device and the Activity restarts, your application must re-fetch the data, which could be slow. What you can do instead is implement
onRetainNonConfigurationInstance()
to return an object carrying your data and then retrieve the data when your Activity starts again withgetLastNonConfigurationInstance()
.Look at here for more info Retaining an Object During a Configuration Change you can also find example over their..
Thanks..
When you rotate the device your Activity will re-create and all the variables will be re-initialized. So, in that case if you want to some values to remain same on Rotation also you can store their state using the
onSaveInstanceState()
and you can restore inonCreate()
again by checking Bundle is not null.Where as
onConfigurationChanged()
will be called when you rotate the Device(Note that this will only be called if you have selected configurations you would like to handle with theconfigChanges
attribute in your manifest). From the parameter you will get the new device configuration.If you don't want your Activity to get re-created on rotation of Device then you have to add this line to your activity tag in the
AndroidManifest
file.