Runtime Configuration Changes

Android handles runtime changes to the language, location, and hardware by terminating and restarting each application and reloading the resource values.

This default behavior isn't always convenient or desirable, particularly as some configuration changes (like those to screen orientation and keyboard availability) can occur as easily as a user can rotate the

device or slide out the keyboard. You can customize your application's response to such changes by detecting and reacting to them yourself.

To have an Activity listen for runtime configuration changes, add an android:configChanges attribute to its manifest node, specifying the configuration changes you want to handle.

The following list describes the configuration changes you can specify:

> orientation The screen has been rotated between portrait and landscape.

> keyboardHidden The keyboard has been exposed or hidden.

> fontScale The user has changed the preferred font size.

> locale The user has chosen a different language setting.

> keyboard The type of keyboard has changed; for example, the phone may have a 12-key keypad that flips out to reveal a full keyboard.

> touchscreen or navigation The type of keyboard or navigation method has changed. Neither of these events should normally happen.

In certain circumstances multiple events will be triggered simultaneously. For example, when the user is sliding out a keyboard most devices will fire both the keyboardHidden and orientation events.

You can select multiple events you wish to handle yourself by separating the values with a pipe (|).

Listing 3-5 shows an activity node declaring that it will handle changes in screen orientation and keyboard visibility.

LISTING 3-5: Activity definition for handling dynamic resource changes

<activity android:name=".TodoList"

android:label="@string/app_name" android:theme="@style/TodoTheme"

android:configChanges="orientation|keyboardHidden"/>

Adding an android:configChanges attribute suppresses the restart for the specified configuration changes, instead triggering the onConfigurationChanged method in the Activity. Override this method to handle the configuration changes, using the passed-in Configuration object to determine the new configuration values, as shown in Listing 3-6. Be sure to call back to the superclass and reload any resource values that the Activity uses, in case they've changed.

Available for download on Wrox.com

LISTING 3-6: Handling configuration changes in code

Available for @Override download on public void onConfigurationChanged(Configuration _newConfig) { Wrox.com super.onConfigurationChanged(_newConfig);

continues

LISTING 3-6 (continued)

if (_newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { [ ... React to different orientation ... ]

if (_newConfig.keyboardHidden == Configuration.KEYBOARDHIDDEN_NO) { [ ... React to changed keyboard visibility ... ]

When onConfigurationChanged is called, the Activity's Resource variables will have already been updated with the new values so they'll be safe to use.

Any configuration change that you don't explicitly flag as being handled by your application will cause your Activity to restart, without a call to onConfigurationChanged.

Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide


Post a comment