"Turn" onconfigurationchanged

Source: Internet
Author: User
Tags locale

http://blog.csdn.net/xiaodongvtion/article/details/679938 reprinted from the article.

Note: The Onconfigurationchanged event is not only triggered by changes in the screen orientation, but other system settings can be triggered, such as opening or hiding the keyboard.

When our screen orientation changes, we can trigger the Onconfigurationchanged event. We need to do the following for the current activity to capture this event.

First: Permission statement:

<uses-permission android:name= "Android.permission.CHANGE_CONFIGURATION" ></uses-permission>

The API says that this permission allows us to change the configuration information, but we can change the screen direction of the program does not use this permission, is it conflicting? Here we can say that when we declare this permission, the system allows us to capture and modify some configuration information by rewriting the Onconfigurationchanged method in activity.

Second: Declare the type of event to be captured by the activity,

Such as

<activity
Android:name= ". Ex05_23 "
Android:label= "@string/app_name"
android:configchanges= "Orientation|keyboard" >
<intent-filter>
<action android:name= "Android.intent.action.MAIN"/>
<category android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

It is important to declare the Android:configchanges property, which specifies the type of event that we can capture in the program, with multiple event types separated by |

If there is no orientation, then we will not be able to capture the screen change event in the program.

Third:

Rewrite the Onconfigurationchanged method in the activity.

For example:


@Override
public void onconfigurationchanged (Configuration newconfig) {
When the screen layout mode is horizontal when the new setting
if (newconfig.orientation = = Activityinfo.screen_orientation_landscape)
{
TODO Some actions
}
Super.onconfigurationchanged (Newconfig);
}

To prevent OnCreate from being called again, first, you need to set android:configchanges, and the optional attributes are as follows:

Value Description
"MCC" The IMSI Mobile Country Code (MCC) has Changed-that are, a SIM hasbeen detected and updated the MCC. Mobile country number, consisting of three digits, each country has Their own independent MCC, can identify the mobile phone users of the country.
"MNC" The IMSI Mobile Network Code (MNC) has Changed-that is, a SIM hasbeen detected and updated the MNC. Mobile network number, in a country or region, used for district breakup The service provider of the machine user.
"Locale" The locale has changed-for example, the user have selected a new language that text should is Displayed in. The user's location changes 。
"Touchscreen" The touchscreen has changed. (This should never normally happen.)
"Keyboard" The keyboard type has changed-for example, the user have plugged in an external keyboard. The keyboard mode changes, for example: User access to external keyboard input.
"Keyboardhidden" The keyboard accessibility has changed-for example, the user have slid the keyboard out to expose it. User opens the phone hardware keyboard
"Navigation" The navigation type has changed. (This should never normally happen.)
"Orientation" The screen orientation have changed-that is and the user has rotated the device. Equipment rotation, landscape display and vertical display mode switching.
"Fontscale" The font scaling factor has changed-that are, the user has selected a new global font size. Global Font Size scaling changes

Here about the screen rotation does not need to call OnCreate again is should be set android:configchanges= "Orientation|keyboardhidden"
Then reload onconfigurationchanged

The following self-collected collation:

if (Newconfig.orientation==configuration.orientation_landscape) {

Transverse

Setcontentview (R.layout.file_list_landscape);

}else{

Vertical direction

Setcontentview (r.layout.file_list);

}

However, since the Android 3.2 (API 13), after setting the activity's android:configchanges= "Orientation|keyboardhidden", the same will recall the various lifecycles.

Because screen size also starts to change with the device's shift.

So, in cases where the Minisdkversion and Targetsdkversion properties set in Androidmanifest.xml are greater than or equal to 13, if you want to prevent the program from reloading the activity at run time, in addition to setting the " Orientation

You must also set the "ScreenSize".

Workaround:

Set android:configchanges= "Orientation|screensize" in Androidmanifest.xml

No screen switching

The activity is restarted every time the screen orientation is switched on Android, so you should save the current active state before the activity is destroyed and load the configuration when the activity is re-create, so the in-progress game will not restart automatically.

Add the android:screenorientation= "Landscape" attribute to the Androidmanifest.xml activity (activity to disable diversion) (Landscape is landscape, Portrait is portrait).

from:http://blog.csdn.net/chencehnggq/article/details/23736869

"Turn" onconfigurationchanged

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.