Tech tip of this week

Android Configuration changes

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.

Normally upon configuration change, the current activity is destroyed automatically (the methods onPause(), onStop(), and onDestroy() are called) and a new activity is created (the methods onCreate(), onStart(), and onResume() are called) for the new configuration.


Any change to the GUI for the new configuration, if necessary, should be done manually.

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"

No comments:

Post a Comment