Android onconfigurationchanged (Configuration cfg) does not trigger the problem

Source: Internet
Author: User

1.android:configchanges= use of "Orientation|keyboardhidden"

When the activity is added with the android:configchanges= "Keyboardhidden|orientation" attribute, the activity is not restarted. Just call onconfigurationchanged ( Configuration newconfig). This way you can adjust the display in this method.

In the XML file can be configured configchanges can also be dynamically configured in the code

Attention:

1, do not set the activity of the android:configchanges, the screen will recall the various life cycle, cut across the screen will be executed once, cut the vertical screen will be executed twice
2, set the activity android:configchanges= "orientation", the screen will recall the various life cycle, cut horizontal, vertical screen will only be executed once
3, set the activity android:configchanges= "Orientation|keyboardhidden", the screen will not recall the various life cycle, will only execute onconfigurationchanged method

but: 1) This method triggers the call to Onconfigurationchanged (Configuration newconfig) when the version android:targetsdkversion<=12 screen is switched

<uses-sdk
Android:minsdkversion= "12"
android:targetsdkversion= "/>"

2) in version 13+ above must add "|screensize", that is android:configchanges= "Orientation|keyboardhidden|screensize" Will trigger Onconfigurationchanged (Configuration newconfig) This method

<uses-sdk
Android:minsdkversion= "12"
Android:targetsdkversion= "/>"

2. The following is the code for screen switching:

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);
}

Overriding the Chonconfigurationchanged (Configuration newconfig) method triggers the call

@Override
public void onconfigurationchanged (Configuration cfg) {
Super.onconfigurationchanged (CFG);

Toast.maketext (Getsysteminfoactivity.this, "Test:" + (Cfg.orientation==configuration.orientation_landscape? ") Horizontal screen ":" Vertical Screen "), Toast.length_short). Show ();

}

3. Turn off the soft keyboard input Method when the screen switch pops up<activity ... Android:windowsoftinputmode= "Adjustunspecified|statehidden"/>or call the code inside the code to close the IME soft keyboard:

Inputmethodmanager Minputmethodmanager = (inputmethodmanager) getsystemservice (Input_method_service);
Minputmethodmanager.hidesoftinputfromwindow (This.getcurrentfocus (). Getwindowtoken (), 0);

Attach (Open Input Method code):

Inputmethodmanager InputManager =
(Inputmethodmanager) Getsystemservice (Context.input_method_service);
Inputmanager.showsoftinput (p, 0);

Why onconfigurationchanged not be called?

The English text is as follows:


Caution:beginning with Android 3.2 (API level), the ' screen size ' also changes when the device switches between Portra It and landscape orientation. Thus, if want to prevent runtime restarts due to orientation change when developing for API level or higher (as Dec Lared by the minsdkversion and Targetsdkversion attributes), you must include the ' screensize ' value in addition to the ' O ' Rientation "value. That's, you must decalare android:configchanges= "Orientation|screensize". However, if your application targets API level/lower, then your activity all handles this configuration change it Self (This configuration change does not restart your activity, even if 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 at targeting API level 13+

Reference:

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 (Turn off IME reference address)

Android onconfigurationchanged (Configuration cfg) does not trigger the problem

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.