As mentioned above, the life cycle of the activity when the screen is rotated. When the amount of data is large and the screen is rotated, two methods are generally used to avoid the new layout of the activity. First, set the screenorientation attribute to avoid the new layout during rotation:
If you do not want the software to switch between the horizontal and vertical screens, the easiest way is to do so in the androidmanifest of the project. in XML, add the Android: screenorientation attribute to the activity you specified. It has the following parameters:
"Unspecified"
The default value is used by the system to determine the display direction. The rule is related to the device, so different devices have different display directions.
"Landscape"
Horizontal Screen Display (longer than width)
"Portrait"
Portrait display (height to width)
"User"
User's current preferred direction
"Behind"
Consistent with the direction of the activity under the activity (in the activity stack)
"Sensor"
It is determined by a physical sensor. If you rotate the device, the screen will be switched horizontally and vertically.
"Nosensor"
Ignore the physical sensor so that it will not be changed as the user rotates the device (except for the "unspecified" setting ).
For example:
<Activity
Android: Label = "@ string/app_name"
Android: Name = ". activitylifetestactivity"
Android: screenorientation = "Landscape">
<Intent-filter>
<Action Android: Name = "android. Intent. Action. Main"/>
<Category Android: Name = "android. Intent. Category. launcher"/>
</Intent-filter>
</Activity>
You can also use the setrequestedorientation (activityinfo. screen_orientation_portrait) method in the application to control the screen direction.
Through this method, the lifecycle of the activity is still normal, and the current activty will still be killed and then be created again.
Type 2: configure the Android: configchanges attribute
If you do not need to load the data again, you can go to androidmanifest. add Android: configchanges = "orientation" to XML. The function of configuring Android: configchanges is as described in the document: specify one or more configuration changes that the activity will handle itself. if not specified, the activity will be restarted if any of these
Configuration changes happen in the system. In this way, the. activity will not repeatedly call oncreate () or even onpause. onresume. Only one onconfigurationchanged (configuration newconfig) will be called ).
If I only set orientation in Android: configchanges, it will still be reloaded. Only when orientation | keyboardhidden is set will it call only one onconfigurationchanged (configuration newconfig)
If you need to reload data, you do not need to make any modifications. However, if you need to save the previous operation content or data during the reload process, you need to save the previous data. Then it is obtained in oncreate () of the activity. Of course, you cannot set Android: configchanges (). Otherwise, the oncreate () method will not be called.
Android: configchanges is not set. When the screen is switched, it will be onpause, onstop, ondestroy, and then oncreate, onstart, onresume, that is, stop first, and then start from oncreate.
After setting Android: configchanges
@ Override
Public void onconfigurationchanged (configuration config ){
Super. onconfigurationchanged (config );
If (this. getresources (). getconfiguration (). Orientation = configuration. orientation_landscape ){
Log. I (TAG, "Landscape ");
} Else if (this. getresources (). getconfiguration (). Orientation = configuration. orientation_portrait ){
Log. I (TAG, "Landscape ");
}
}
The two methods of appeal have basically completed the knowledge required for screen switching.
From: http://blog.sina.com.cn/s/blog_9f233c0701012rdt.html