Activity screen switching, Activity screen Switching
Preface
In development, you often need to handle screen switching. How to handle this problem first, first look at the lifecycle
Statement
Two functions need to be called back during Activity horizontal/vertical screen switching. Therefore, the two functions are regarded as part of the lifecycle of Activity horizontal/vertical screen switching. The two functions are as follows:
OnSaveInstanceState (Bundle outState): stores data when the Activity is about to be destroyed.
OnRestoreInstanceState (Bundle savedInstanceState): Retrieves data during Activity reconstruction or restoration.
Landscape screen switching lifecycle 1. Start the program to enter the Activity Interface
@ Override
Public void onConfigurationChanged (Configuration newConfig ){
Super. onConfigurationChanged (newConfig );
If (newConfig. orientation = 1) // portrait Screen
If (newConfig. orientation = 2) // landscape
}
2. When the screen changes to a horizontal screen, the system will re-call the onCreate method of the Activity to check the current direction in onCreate, then, let your setContentView load different layout xml files.
If (this. getResources (). getConfiguration (). orientation = Configuration. ORIENTATION_LANDSCAPE ){
Log. I ("info", "landscape"); // landscape
} Else if (this. getResources (). getConfiguration (). orientation = Configuration. ORIENTATION_PORTRAIT ){
Log. I ("info", "portrait"); // portrait Screen
}
Note: This method does not set onConfigurationChanged in AndroidManifest. xml to repeat the lifecycle.
Layout File Settings for horizontal and vertical screen Switching
If you want the software to switch between portrait and portrait screens, the height and width of the portrait screen may be converted, and different la s may be required. You can switch the layout using the following methods:
(1) Create the layout-land and layout-port directories under the res directory. The names of the corresponding layout files remain unchanged, such as main. xml. Layout-land is the layout of the horizontal screen, and layout-port is the layout of the vertical screen. If you do not need to worry about it, the simulator will automatically search for it.
(2) In the above screen identification, if the screen changes, in onCreate () or onConfigurationChanged () to determine the direction, you can re-setContentView in the corresponding method to load different layout xml layout files.
Data storage and reading during horizontal and vertical screen Switching
In addition, the Activity is restarted every time the screen is switched on in android. Therefore, you should save the status of the current Activity before the Activity is destroyed and load the configuration when the Activity is created again, in progress, the game will not automatically restart!
Activity data storage
(1) If the Destory of the Activity or the destroyed and Recreated of the screen are caused by insufficient system resources, the system will record the Activity when the user returns to the Activity, the system uses the record data (instance state) which is the key-value pairs in the Bundle object. By default, the system uses Bundle to save information.
(2) In order to save more data to the instance state, rewrite the callback function onSaveInstanceState (Bundle outState). The system will pass the Bundle object when the Activity is Destory exceptions, in this way, we can add additional information to the Bundle and save it to the system. If the system wants to recreate the Activity instance after the Activity is Destory, the previous Bundle object (system) will be passed to your activity
(3) When the Activity starts to stop, the system will call onSaveInstanceState (Bundle outState). The Activity can use a set of key-value pairs to save status information. This method saves the status information of the Activity view by default, such as the text in the EditText component or the sliding position of the ListView.
Activity data recovery
(1) When the Activity is rebuilt from Destory, we can restore the Saved state from the Bundle of the Activity passed by the system. The onCreate () and onRestoreInstanceState () callback methods receive the same Bundle, which contains the same instance status information.
(2) because the onCreate () method will be called when a new Activity instance is created for the first time and when the Destory instance is created before it is re-created, we must check whether the Bundle object is null before trying to read it. If it is null, the system creates a new Activity instance instead of restoring the previously Destory Activity.
(3) You can also implement onRestoreInstanceState () instead of restoring data in the onCreate method. The onRestoreInstanceState () method will be executed after the onStart () method. The system will only call onRestoreInstanceState () when the State information to be restored exists. Therefore, you do not need to check whether the Bundle is null.