lomza lomza - 2 months ago 10
Android Question

Android configuration changed

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.

Answer

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 in onCreate() again by checking Bundle is not null.

if(savedInstanceState != null){
            // get the restore value from the Bundle
        }

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 the configChanges 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.

android:ConfigChanges="keyboardHidden|orientation"