Solution to not calling onConfigrationChange in andriod 4.0 or later versions

Source: Internet
Author: User

Solution to not calling onConfigrationChange in andriod 4.0 or later versions

First, let's take a look at how the onConfigrationChange method was called in versions earlier than Android4.0:

1. First, add the permission in the manifest. xml file:

2. Configure the property: android: configChanges = "orientation | keyboardHidden" for the activity in the manifest. xml file"

3. Finally, inherit the method in the Activity class:

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

// You can switch the layout of the screen or any other operations you want.

If (newConfig. orientation = Configuration. ORIENTATION_LANDSCAPE) {textView. setText ("current screen is landscape");} else {textView. setText ("current screen is portrait ");}

}


Later, in version 4.2, various attempts did not call the onConfigurationChanged method, which was very strange. I understand that there is no doubt about calling this method when I was working on a project. What is the reason? This problem has been confusing for a long time. I will not talk about it anymore. Simply put, the solution is very simple. Modify the attributes of the manifest. xml file and add a screenSize to the end, as shown below:

Android: configChanges = "orientation | keyboardHidden | screenSize"

Then, it was really a problem with the Android version. Haha! This problem is finally solved.


Note the following:

For android: configChanges attributes, it is generally considered as follows:

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, you must note that ScreenSize must be added for Versions later than 4.0. Oh!


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.