Android notes: onConfigurationChanged

Source: Internet
Author: User

Engaged in Android development, it is inevitable that some advertisement sdks will be embedded in the application. After many sdks are embedded, almost every requirement is found in AndroidManifest. the xml declarative Activity advertisement SDK requires that such an attribute be added:

Copy codeThe Code is as follows: android: configChanges = "orientation | keyboard | keyboardHidden"

You can see from the Android API that android: onConfigurationChanged actually corresponds to the onConfigurationChanged () method in the Activity. The appeal code added to AndroidManifest. xml indicates that the onCreate () method is not executed when the screen direction is changed, the software disk is popped up, and the soft keyboard is hidden, but the onConfigurationChanged () method is executed directly (). If you do not declare this code segment, the onCreate () method will be executed according to the Activity lifecycle, while the onCreate () method usually performs initialization before the display. Therefore, if the onCreate () method is executed for all operations such as changing the screen direction, it may cause repeated initialization and reduce program efficiency, in addition, data may be lost due to repeated initialization. This must be avoided by ten millions.

To understand this problem, I wrote a Demo to observe the execution results.

Copy codeThe Code is as follows: public class ConsoleActivity extends Activity {
Private String str = "0 ";

Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
// Simulate data Initialization
Str = "1 ";
Log. e ("FHT", "onCreate:" + str );
}

@ Override
Protected void onStart (){
Super. onStart ();
// Data changes after analog display
Str = (new Date (). getTime () + "";
Log. e ("FHT", "onStart:" + str );
}

@ Override
Public void onConfigurationChanged (Configuration newConfig ){
Super. onConfigurationChanged (newConfig );
Log. e ("FHT", "onConfigurationChanged:" + str );
}
}

The running result is as follows:

It can be seen that when the screen is flipped three times, the three folds are not re-entered into the onCreate () method, so the str value can be continued. If AndroidManifest is removed. the code about onConfigurationChanged in xml will change the execution sequence of the program, and every change in the screen direction will cause the reset of the str value. This is not expected in most development processes.

In addition, note that: super. onConfigurationChanged (newConfig) in the onConfigurationChanged () method must not be omitted. Otherwise, an android. app. SuperNotCalledException exception will be thrown.

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.