Android Learning notes-about onconfigurationchanged

Source: Internet
Author: User

Android Development, inevitably embedded in the application of some advertising SDK, after embedding a number of SDKs, found that almost every request in the Androidmanifest.xml declaration activity of the Advertising SDK will be asked to add this sentence attribute:

Android:configchanges= "Orientation|keyboard|keyboardhidden" by checking the Android API to learn Android:o Nconfigurationchanged actually corresponds to the onconfigurationchanged () method in the activity. The implication of adding an appeal code in Androidmanifest.xml is that when you change the orientation of the screen, eject the software tray, and hide the soft keyboard, the OnCreate () method is not executed, but the onconfigurationchanged () is executed directly. If this code is not declared, the OnCreate () method is executed once according to the activity's life cycle, and the OnCreate () method usually does some initialization before the display. So if you change the orientation of the screen to execute 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 requirement that must be avoided.

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

 1 public class Consoleactivity extends Activity {2     private String str = "0"; 3  4     protected void OnCreate (Bundle savedinstancestate) {5         super.oncreate (savedinstancestate); 6 & nbsp      //Analog data Initialization 7         str = "1"; 8         LOG.E ("FHT", "onCreate:" + str); 9    }10       @Override14     protected void OnStart () {        Super.onstart (), +        //analog display, data changes (      &NBS) P str = (new Date ()). GetTime () + "";         LOG.E ("FHT", "OnStart:" + str),    }20 21 22     @Override24     public void onconfigurationchanged (Configuration newconfig) {+         super.onconfigurationchanged (newconfig),         LOG.E ("FHT", " Onconfigurationchanged: "+ str);    }28}

As you can see, when the screen orientation has been flipped three times, three flips did not re-enter the OnCreate () method, so the value of STR is continued, If you remove the relevant code for onconfigurationchanged in Androidmanifest.xml, the execution order of the program will change, each time the screen orientation changes will cause the STR value reset. This is what most of the development process does not want to see.

It is also important to note that the Onconfigurationchanged () method: Super.onconfigurationchanged (Newconfig), must not be omitted, Otherwise, a: exception is thrown.

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: 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.