Android notes: onconfigurationchanged detailed _android

Source: Internet
Author: User

Engaged in Android development, will inevitably embed in the application of some advertising SDK, embedded in a large number of SDK, found that almost every request in Androidmanifest.xml to declare the activity of the Ad SDK will be required to add a note of this attribute:

Copy Code code as follows:

Android:configchanges= "Orientation|keyboard|keyboardhidden"

By looking up the Android API, you can tell that android:onconfigurationchanged actually corresponds to the onconfigurationchanged () method in the activity. Adding an appeal code in Androidmanifest.xml means that when you change the screen orientation, eject the software disk, and hide the soft keyboard, you no longer execute the OnCreate () method, but instead execute the onconfigurationchanged () directly. If this code is not stated, the OnCreate () method is executed one time according to the activity lifecycle, and the OnCreate () method usually does some initialization before it is displayed. So if you change the direction of the screen to perform the OnCreate () method, it is possible to create duplicate initialization, reduce the efficiency of the program is inevitable, and more likely because of repeated initialization of data loss. This is a necessity to avoid.

To understand this, a demo was written to observe the results of the execution.

Copy Code code 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 the simulation is displayed
str = (new Date ()). GetTime () + "";
LOG.E ("FHT", "OnStart:" + str);
}



@Override
public void onconfigurationchanged (Configuration newconfig) {
Super.onconfigurationchanged (Newconfig);
LOG.E ("FHT", "onconfigurationchanged:" + str);
}
}

The results of the operation are as follows:

As you can see from the diagram above, when the screen direction three times flipped, three flips did not re-enter the OnCreate () method, so the value of STR continued, If you remove the code related to onconfigurationchanged in Androidmanifest.xml, the order of execution of the program will change, and each change in the direction of the screen will cause the STR value to be reset. This is what most of the development process does not want to see.

In addition to note is the Onconfigurationchanged () method: Super.onconfigurationchanged (Newconfig), must not be omitted, Otherwise, a Android.app.SuperNotCalledException exception is 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.