Reprint: http://www.cnblogs.com/bluestorm/p/3622444.html
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 looking at the Android API, you know that android:onconfigurationchanged actually corresponds to the onconfigurationchanged () method in 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 classConsoleactivityextendsActivity {2 PrivateString str = "0";3 4 protected voidonCreate (Bundle savedinstancestate) {5 Super. OnCreate (savedinstancestate);6 //Analog Data Initialization7str = "1";8LOG.E ("FHT", "OnCreate:" +str);9 }Ten One A - @Override - protected voidOnStart () { the Super. OnStart (); - //after the simulation shows, the data changes -str = (NewDate ()). GetTime () + ""; -LOG.E ("FHT", "OnStart:" +str); + } - + A at @Override - Public voidonconfigurationchanged (Configuration newconfig) { - Super. onconfigurationchanged (newconfig); -LOG.E ("FHT", "onconfigurationchanged:" +str); - } -}
The results of the operation are as follows:
As you can see, when the screen orientation has been flipped three times, three flips have not re-entered the OnCreate () method, so the value of STR is continued, if the removal Androidmanifest.xml in the relevant code about onconfigurationchanged, the execution order of the program will change, each time the screen direction 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 it will lead Hair: Android.app.SuperNotCalledException exception.
Android: About Onconfigurationchanged () Introduction (GO)