In the case of Android, when the "screen orientation" or "keyboard display hidden" Changes, the current activity is destroyed and a new activity is created. If you do not want to recreate the activity instance, you can configure it in Androidmanifest.xml
android:configchanges= "Keyboardhidden|orientation" >
This will not destroy the rebuild, after configuring this property, the Android:configchanges property captures the "screen orientation" and "keyboard display hidden" Changes, and when these changes are captured, the activity's onconfigurationchanged is called ( Method
However, the above configuration only works in versions prior to android4.0, which does not work on Android 4.0 and must be added screensize, which means that the Android 4.0 version must be configured like this
Android:configchanges= "Keyboardhidden|orientation|screensize"
Here we combine examples to say orientation and onconfigurationchanaged methods
Code:
public class Mainactivity extends Appcompatactivity {private static final String TAG = "Owen"; When activity is created, call @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancest ATE); Setcontentview (R.layout.activity_main); LOG.I (TAG, "OnCreate method implemented"); //activity was called when it was created or returned back to the foreground from the background @Override protected void OnStart () {Super.onstart (); LOG.I (TAG, "OnStart method implemented"); //activity is called @Override protected void Onrestart () {Super.onrestart () when returning from the background to the foreground; LOG.I (TAG, "Onrestart method implemented"); //activity is called @Override protected void Onresume () {super.onresume () when it is created or has been overwritten and returned back to the foreground from the background; LOG.I (TAG, "Onresume method implemented"); //activity is called @Override protected void OnPause () {super.onpause () when it is overwritten to the following or lock screen; LOG.I (TAG, "OnPause method implemented"); }//exits the current activity or jumps to the new activity when called @Override protected void OnStop () {super.onstop (); LOG.I (TAG, "executed the OnStop partyLaw "); }//is called when exiting the current activity and the activity ends after the call @Override protected void OnDestroy () {Super.ondestroy (); LOG.I (TAG, "OnDestroy method implemented"); }//When the android:configchanges= "orientation" is specified, onconfigurationchanged is called when the direction changes, and the activity no longer destroys the rebuild @Override public voi D onconfigurationchanged (Configuration newconfig) {super.onconfigurationchanged (newconfig); Switch (newconfig.orientation) {case configuration.orientation_portrait://vertical screen log.i (TAG, "vertical screen"); Break Case configuration.orientation_landscape://Horizontal Screen log.i (TAG, "horizontal screen"); Default:break; } }}
When you do not configure android:configchanges, when you switch between a horizontal screen and a vertical screen (turn on the "auto-rotate screen" option in your phone's settings) logs such as:
From the log above you can see that the activity performed the destruction of the rebuild process, onconfigurationchanaged is not called
If you add android:configchanges= "keyboardhidden|orientation|screensize" then rotate the log as follows:
As you can see from the log above, the activity did not perform the process of destroying the rebuild, and the Onconfigurationchanaged method was called
Ext.: http://blog.csdn.net/owenchan1987/article/details/53004474
A brief introduction to the properties of Android:configchanges