Android onConfigurationChanged (Configuration cfg) cannot trigger the problem. androidproguard. cfg

Source: Internet
Author: User

Android onConfigurationChanged (Configuration cfg) cannot trigger the problem. androidproguard. cfg

1. android: configChanges = "orientation | keyboardHidden" Usage

When the android: configChanges = "keyboardHidden | orientation" attribute is added to the activity, the activity will not be restarted. Instead, the onConfigurationChanged (Configuration newConfig) is called. In this way, the display mode can be adjusted.

You can configure configChanges in the xml file or dynamic configuration in the code.

Note:

1. When the android: configChanges of the Activity is not set, the life cycle of the screen is re-called, the screen is executed once, and the screen is split twice.
2. When setting the Activity's android: configChanges = "orientation", the screen will be switched to call each lifecycle, and the screen will be executed only once
3. When the android: configChanges = "orientation | keyboardHidden" of the Activity is set, the live cycle is not re-called and only the onConfigurationChanged method is executed.

However: 1) The onConfigurationChanged (Configuration newConfig) method is called only when the android: targetSdkVersion <= 12 screen switch is used.

<Uses-sdk
Android: minSdkVersion = "12"
Android: targetSdkVersion = "12"/>

2) You must add "| screenSize" to Versions later than 13, that is, android: configChanges = "orientation | keyboardHidden | screenSize" to trigger onConfigurationChanged (Configuration newConfig ).

<Uses-sdk
Android: minSdkVersion = "12"
Android: targetSdkVersion = "13"/>

2. The code for screen switching is as follows:

 

Configuration cfg = getResources (). getConfiguration ();

If (cfg. orientation = Configuration. ORIENTATION_LANDSCAPE ){
GetSystemInfoActivity. this. setRequestedOrientation (ActivityInfo. SCREEN_ORIENTATION_PORTRAIT );

}
If (cfg. orientation = Configuration. ORIENTATION_PORTRAIT ){
GetSystemInfoActivity. this. setRequestedOrientation (ActivityInfo. SCREEN_ORIENTATION_LANDSCAPE );
}

 

Override the chonConfigurationChanged (Configuration newConfig) method to trigger the call

@ Override
Public void onConfigurationChanged (Configuration cfg ){
Super. onConfigurationChanged (cfg );

Toast. makeText (GetSystemInfoActivity. this, "test:" + (cfg. orientation = Configuration. ORIENTATION_LANDSCAPE? "Landscape": "Landscape"), Toast. LENGTH_SHORT). show ();

}

 

3. When the screen is switched, the keyboard input method is closed.<Activity... android: windowSoftInputMode = "adjustUnspecified | stateHidden"/> or call the code in the code to disable the soft keyboard of the input method:

InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService (INPUT_METHOD_SERVICE );
MInputMethodManager. hideSoftInputFromWindow (this. getCurrentFocus (). getWindowToken (), 0 );

Attachment (open the input method code ):

InputMethodManager inputManager =
(InputMethodManager) getSystemService (Context. INPUT_METHOD_SERVICE );
InputManager. showSoftInput (p, 0 );

Why is onConfigurationChanged not called?

 

The original English text is as follows:


Caution: Beginning with Android 3.2 (API level 13), the "screen size" also changes when the device switches between portrait and landscape orientation. thus, if you want to prevent runtime restarts due to orientation change when developing for API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes ), you must include the "screenSize" value in addition to the "orientation" value. that is, you must decalare android: configChanges = "orientation | screenSize ". however, if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device ).

 

(From http://developer.android.com/guide/topics/resources/runtime-changes.html)

TL; DR: add "| screenSize" to configChanges when targeting API level 13 +

 

Refer:

1. http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2012/1106/516.html

2. http://www.cnblogs.com/xiaokang088/p/3540189.html

3. http://blog.csdn.net/songshimvp1/article/details/50109879

4. http://www.cnblogs.com/androidez/archive/2013/04/09/3011399.html (close Input Method reference address)

Related Article

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.