A summary of Android screen switching and activity restart cycle

Source: Internet
Author: User

Ways to disable the Android screen and restart activity when switching screens

1. Add the android:screenorientation= "Landscape" attribute to the Androidmanifest.xml activity configuration (Landscape is landscape, Portrait is portrait). Such as:

<activity android:name= ". Contactsmanageractivity "android:label=" @string/app_name "android:screenorientation=" Portrait ">
< intent-filter>
<action android:name= "Android.intent.action.MAIN" >
<category Android:na Me= "Android.intent.category.LAUNCHER" >
</category></action></intent-filter>
</ Activity>


2. General screen switching, the activity to restart, in order to avoid restarting, you can add android:configchanges= "orientation" attribute for the activity, The Onconfigurationchanged () method is then overwritten in the activity, for example:

public void onconfigurationchanged (Configuration newconfig) {
//TODO auto-generated method Stub
if (Newco        Nfig.orientation==configuration.orientation_landscape) {
setcontentview (r.layout.imageswitch);
Horizontal screen
} else {
setcontentview (r.layout.editcontact);//Vertical screen
}
Super.onconfigurati OnChanged (Newconfig);
}


This can be achieved without restarting the activity, to achieve the screen switch.



Solve Android phone screen switch

When the screen switches in Android, the life cycle of the activity is reloaded (indicating that the current activity is destroyed, but reloading), how can the current activity not be destroyed when the screen is switched on and off?

1. Set configchanges properties for the activity in Androidmanifest.xml,

Application android:icon= "@drawable/icon" android:label= "@string/app_name" > <activity android:name=
". Mainactivity "
android:label=" @string/app_name "android:configchanges=" Orientation|keyboardhidden ">
<intent-filter>
<action android:name= "Android.intent.action.MAIN"/>
<category android: Name= "Android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</ Application>


Configchanges has the following options: 1. Orientation: The screen rotates between portrait and transverse, 2.  Keyboardhidden: Keyboard display or hidden, 3.fontScale: User changed the preferred font size 4.locale: The user selected a different language setting, 5. Keyboard: Keyboard type changes, such as mobile phone from 12 keyboard switch to full keyboard 6. Touchscreen or navigation: changes in keyboard or navigation patterns,

If the Keyboardhidden option is missing, the activity cannot be prevented from being destroyed, and events in the subsequent Onconfigurationchanged event that can only capture a vertical screen to a horizontal screen cannot capture a horizontal screen and a vertical screen


2. Rewrite in the corresponding activity: Onconfigurationchanged method:

public class mainactivity extends activity {    private 
textview textview;      @Override     public void oncreate (bundle 
savedinstancestate)  {        super.oncreate (savedInstanceState);
        setcontentview (R.layout.main);
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;LOG.I ("--main--",  "onCreate");
        textview= (TextView) Findviewbyid (r.id.tv_id);
&NBSP;&NBSP;&NBSP;&NBSP}                @Override
    public void onconfigurationchanged (Configuration newconfig)  {
        super.onconfigurationchanged (NewConfig); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;LOG.I ("--main--",  "onconfigurationchanged");         if (Newconfig.orientation==configuration.orientation_landscape) {            textview.settext ("current screen is horizontal screen");          }else{          
  textview.settext ("Current screen is vertical screen");         }    &nbsp}     }



The layout file is a simple textview.

The effect is as follows:

Log printing:


From the log can be analyzed in the screen switch when the activity did not destroy, of course, you can also run the project in the OnCreate method to make a breakpoint, the implementation of the discovery OnCreate method is just beginning to enter the execution, screen at the end screen switch, will not be in execution, Therefore can be in the Onconfigurationchanged method lower point article!

Note: If the item does not require a screen switch, it can be set to

1. android:screenorientation= "Portrait" is always displayed as a vertical screen
2. android:screenorientation= "Landscape" is always displayed in horizontal screen

The above configuration file sets the screen at the bottom, and the following is the code to control the screen:

Private onclicklistener onclick=new onclicklistener ()  {     @Override     public void onclick (view v)  {         //set screen to horizontal screen         if (V==butlandscrpe) {             mainactivity.this.setrequestedorientation (
Activityinfo.screen_orientation_landscape);         //to set the screen to the vertical screen         } else{            
MainActivity.this.setRequestedOrientation (activityinfo.screen_orientation_portrait);
        }     }; Monitor changes to System settings @Override public void onconfigurationchanged (configuration newconfig)  { 
   super.onconfigurationchanged (Newconfig);     string message=newconfig.orientation==configuration.orientation_landscape ?  "
The screen is set to: Horizontal screen " : " screen set to: Vertical screen;
    showtoast (message); }




the life cycle of the activity at the same screen switch

1. Create a new activity and print out each life cycle
2, run the activity, get the following information
Oncreate-->onstart-->onresume-->
3, press CRTL+F12 switch Cheng when
Onsaveinstancestate-->onpause-->onstop-->ondestroy-->oncreate-->onstart--> Onrestoreinstancestate-->onresume-->
4, and then press CRTL+F12 switch to the vertical screen, the discovery printed two times the same log
Onsaveinstancestate-->onpause-->onstop-->ondestroy-->oncreate-->onstart--> Onrestoreinstancestate-->onresume-->onsaveinstancestate-->onpause-->onstop-->ondestroy--> Oncreate-->onstart-->onrestoreinstancestate-->onresume-->
5, modify the Androidmanifest.xml, add the activity android:configchanges= "orientation", perform step 3
Onsaveinstancestate-->onpause-->onstop-->ondestroy-->oncreate-->onstart--> Onrestoreinstancestate-->onresume-->
6, and then perform step 4, found that no longer print the same information, but print more than one line onconfigchanged
Onsaveinstancestate-->onpause-->onstop-->ondestroy-->oncreate-->onstart--> Onrestoreinstancestate-->onresume-->onconfigurationchanged-->
7. Change the android:configchanges= "orientation" of step 5 into android:configchanges= "Orientation|keyboardhidden", and perform step 3, Just print onconfigchanged
Onconfigurationchanged-->
8, the implementation of step 4
Onconfigurationchanged-->onconfigurationchanged-->
Summarize:
1, do not set the activity of the android:configchanges, the screen will recall each life cycle, cut the horizontal screen will be executed once, cut the vertical screen will be executed twice
2, set the activity of the android:configchanges= "orientation", cut screen or will recall the life cycle, cut horizontal, vertical screen will only perform once
3, set the activity of the android:configchanges= "Orientation|keyboardhidden", the screen will not recall each life cycle, will only execute the Onconfigurationchanged method


Summarize the life cycle of the entire activity
Add that the current Activity generation event pops up toast and alertdialog and there is no change in the life cycle
The activity runs by pressing the home key (same as being completely overwritten): Onsaveinstancestate--> onpause--> onStop onrestart----->onstart Me
Activity not completely covered just lost focus: OnPause--->onresume


How to make Android switch without destroying current activity

We all know that when the Android screen is transformed horizontally or vertically, it destroys the current activity and then restarts it.
Do not destroy the current activity in order to toggle the screen switch I'll introduce two methods.

method One: Do not allow the horizontal screen switch:

Implementation: There is also a property in the activity related to the screen direction:

android:screenorientation=["Unspecified" | "User" | "Behind" | "Landscape" | "Portrait" | "Sensor" | "Nosensor"]
adds such an attribute to the activity element of Mainifest.xml:
android:screenorientation= "Portrait" regardless of how the phone changes, The activity that owns this property will be the vertical screen display.
android:screenorientation= "Landscape", for horizontal screen display.


//--------------------------------------------------------------------------------------------

Method Two:

First, add the android:configchanges= "Orientation|keyboardhidden" attribute to the Mainifest.xml activity element:

The implication of adding this property is that the application will handle changes to the screen orientation and keyboard status (eject or close) information.

However, changes to other device configuration information are handled by the Android system (destroying the current activity and then restarting a new activity instance)

Then add the processing code for the configuration information change in the activity subclass of the Java code:


-----------------------------------------------------------------------------------------
@Override public void onconfigurationchanged (Configuration newconfig)
{
super.onconfigurationchanged (newconfig);
To detect the orientation of the screen: portrait or Landscape
if (this.getresources (). GetConfiguration (). Orientation = = Configuration.orientation_landscape 
{
//is currently a horizontal screen where additional processing code is added
} else if (This.getresources (). GetConfiguration (). Orientation = = configuration.orientation_portrait)
{
//Current vertical screen, add extra processing code here
}
//detect the status of the physical keyboard: push or close
if ( Newconfig.hardkeyboardhidden) = = Configuration.hardkeyboardhidden_no)
{
//entity keyboard is in the eject state, add additional processing code here
else if (Newconfig.hardkeyboardhidden = = Configuration.hardkeyboardhidden_yes)
{
//entity keyboard is in a closed state, Add extra processing code here
}
}


Related Article

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.