In the Android simulator, the shortcut key "Ctrl + F11/F12" can be used to screen a very simple Activity: package cn.sohu.com; import android. app. activity; import android. OS. bundle; import android. util. log; public class TestActivityActivity extends Activity {@ Override public void onCreate (Bundle savedInstanceState) {Log. I ("TAG", "onCreate () method called"); super. onCreate (savedInstanceState); setContentView (R. layout. main) ;}@ Overrideprotected void onStart () {Log. I ("TAG", "onStart () method called"); super. onStart () ;}@ Overrideprotected void onRestart () {Log. I ("TAG", "onRestart () method called"); super. onRestart () ;}@ Overrideprotected void onResume () {Log. I ("TAG", "onResume () method called"); super. onResume () ;}@ Overrideprotected void onSaveInstanceState (Bundle outState) {Log. I ("TAG", "onSaveInstanceState () method called"); super. onSaveInstanceState (outState) ;}@ Overrideprotected void onRestoreInstanceState (Bundle savedInstanceState) {Log. I ("TAG", "onRestoreInstanceState () method called"); super. onRestoreInstanceState (savedInstanceState) ;}@ Overrideprotected void onPause () {Log. I ("TAG", "onPause () method called"); super. onPause () ;}@ Overrideprotected void onStop () {Log. I ("TAG", "onStop () method called"); super. onStop () ;}@ Overrideprotected void onDestroy () {Log. I ("TAG", "onDestroy () method called"); super. onDestroy ();}}
When switching the screen, we call the following methods in sequence:
05-10 12:01:16. 831: I/TAG (304): onSaveInstanceState () method called
05-10 12:01:16. 831: I/TAG (304): The onPause () method is called.
05-10 12:01:16. 831: I/TAG (304): The onStop () method is called.
05-10 12:01:16. 831: I/TAG (304): onDestroy () method called
05-10 12:01:16. 850: I/TAG (304): The onCreate () method is called.
05-10 12:01:16. 931: I/TAG (304): The onStart () method is called.
05-10 12:01:16. 931: I/TAG (304): onRestoreInstanceState () method called
05-10 12:01:16. 931: I/TAG (304): The onResume () method is called.
It can be seen that the Activity is protected and then destroyed during screen switching. After switching, the Activity is rebuilt and then restored.
We now require that you do not destroy or recreate the Activity during screen switching.
Step 1: add the property android: configChanges = "orientation | keyboardHidden" in AndroidManifest. xml"
Step 2: override the life cycle method onConfigurationChanged (Configuration newConfig) of the activity:
Public void onConfigurationChanged (Configuration newConfig ){
Super. onConfigurationChanged (newConfig );
If (newConfig. orientation = Configuration. ORIENTATION_LANDSCAPE ){
TextView. setText ("Landscape screen now ");
} Else {
TextView. setText ("portrait screen now ");
}
}
In this way, we can set different layout files for different directions on the screen !!!!!!!!!
That is, the layout-land and layout-port directories are created under the res directory. The corresponding layout files remain unchanged and the main. xml is copied to the two directories.
Layout-land is the layout of the horizontal screen, and layout-port is the layout of the vertical screen.