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.