Life cycle change of activity during screen switching

Source: Internet
Author: User

The life cycle of the activity changes for the screen, the activity of the different life cycle changes, their own real machine debugging test, summarized as follows:
Activity class:
Package Com.example.screenorientationchange;import Android.support.v7.app.actionbaractivity;import Android.content.res.configuration;import Android.os.bundle;public class Mainactivity extends ActionBarActivity {@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); System.out.println ("-----------onCreate-----------------");} @Overrideprotected void OnStart () {//TODO auto-generated method Stubsuper.onstart (); System.out.println ("-----------onStart-----------------");} @Overrideprotected void Onrestart () {//TODO auto-generated method Stubsuper.onrestart (); System.out.println ("-----------onrestart-----------------");} @Overrideprotected void Onresume () {//TODO auto-generated method Stubsuper.onresume (); System.out.println ("-----------onresume-----------------");} @Overrideprotected void OnPause () {//TODO auto-generated method Stubsuper.onpause (); System.out.println ("-----------onPause-----------------");} @Overrideprotected void OnStop () {//TODO auto-generated method Stubsuper.onstop (); System.out.println ("-----------onStop-----------------");} @Overrideprotected void OnDestroy () {//TODO auto-generated method Stubsuper.ondestroy (); System.out.println ("-----------OnDestroy-----------------");} @Overridepublic void onconfigurationchanged (Configuration newconfig) {//TODO auto-generated method Stubsuper.onconfigurationchanged (Newconfig); System.out.println ("-----------onconfigurationchanged-----------------");} @Overrideprotected void Onsaveinstancestate (Bundle outstate) {//TODO auto-generated method Stubsuper.onsaveinstancestate (outstate); System.out.println ("-----------onsaveinstancestate-----------------");} @Overrideprotected void Onrestoreinstancestate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.onrestoreinstancestate (savedinstancestate); System.out.println ("-----------onrestoreinstancestate-----------------");}}

When you start the program, the result is printed in the info message (the vertical screen is the default)


When you switch to the horizontal screen, print the information as follows, you can see the re-create an activity,

When you switch back to the vertical screen again, the print information is as follows (below the Purple Line) and again oncreate an activity.



Now let's go to Androidmainifest.xml and configure the android:configchanges= "" parameterlet's talk first. android:configchanges= "" parameter type means:
"MCC" International Mobile Subscriber identification number The country code is changed-----SIM was detected, to update MCC MCC is the mobile user's country code
"MNC" The mobile network number of the International Mobile Subscriber ID is changed------SIM is detected, to update the MNC MNC is a mobile network number, up to two digits, to identify mobile users belong to the mobile communication network
"Locale" The address changed-----The user chose a new language that would show up
"Touchscreen" The touchscreen is changed------usually doesn't happen
"Keyboard" The keyboard has changed----such as the user using an external keyboard
"Keyboardhidden" The usability of the keyboard has changed
"Navigation" Navigation has changed-----usually does not occur
"Screenlayout" The display of the screen has changed------different displays are activated
"Fontscale" Font proportions have changed----Select a different global font
"Uimode" The user's pattern has changed
"Orientation" The screen orientation has changed.
"ScreenSize" The screen size has changed
"Smallestscreensize" The physical size of the screen has changed, such as: Connect to an external screen

































Modify the Androidmanifest.xml to add the activity android:configchanges= "orientation"
    <activity            android:name= ". Mainactivity "            <span style=" Background-color:rgb (255, 255, 153); " >android:configchanges= "Orientation" </span>            android:label= "@string/app_name" >            < intent-filter>                <action android:name= "Android.intent.action.MAIN"/>                <category android:name= " Android.intent.category.LAUNCHER "/>            </intent-filter>     </activity>

then run the program and you can see that the print information is exactly the same as it was before it was configured.

Then re-modify the Androidmanifest.xml to add the activity android:configchanges= "Orientation|screensize"
        <activity            android:name= ". Mainactivity "            android:configchanges=" orientation|screensize "            android:label=" @string/app_name ">            <intent-filter>                <action android:name= "Android.intent.action.MAIN"/>                <category android: Name= "Android.intent.category.LAUNCHER"/>            </intent-filter>        </activity>

The printing results are as follows:
No re-create an activity at this time, just called the Onconfigurationchange method, no matter how you switch, will only invoke this method.


Summary: 1, do not set the activity ofandroid:configchanges= "and set to android:configchanges= "orientation", the results are the same in both cases. All re-oncreate .an activity. (I am a real machine debugging, the system is 4.1, the version setting is android:minsdkversion= "8" android:targetsdkversion= "21"). Baidu on the internet a bit, the reason is this, since the Android3.2 (API 13) system, with the screen size of the switch screens with the change, so,in Androidmanifest.xml setting the Minisdkversion and Targetsdkversion properties greater than 13, set "ScreenSize" In addition to setting "orientation".
2, when set android:configchanges= "orientation|screensize", the switch of the screen will no longer recall the various life cycle, The Onconfigurationchanged method is then executed.







Life cycle change of activity during screen switching

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.