Android screen switch does not restart activity solution _android

Source: Internet
Author: User
Some users will find activity in the switch to the background or layout from the horizontal screen landscape switch to portrait, will switch activity will trigger a OnCreate method, We can add this attribute android:configchanges= "Orientation|keyboardhidden" to the Activit element in Androidmanifest.xml, such as
<activity android:name= ". android123" android:configchanges= "Orientation|keyboardhidden" android:label= "@string /app_name ">
Java code
Copy Code code as follows:

/* Declare the Display object to get the screen wide/high/
Final Display Defaultdisplay = GetWindow (). Getwindowmanager ()
. Getdefaultdisplay ();

Intscreenh = Defaultdisplay.getheight ();
Intscreenw = Defaultdisplay.getwidth ();

/* If for landscape * *
if (Intscreenw > Intscreenh)
{
/* Landscape => Portrait * *
Setrequestedorientation (activityinfo.screen_orientation_portrait);
} else
{
/* Portrait => Landscape * *
Setrequestedorientation (Activityinfo.screen_orientation_landscape);
}
/* Declare the Display object to get the screen wide/high/
Final Display Defaultdisplay = GetWindow (). Getwindowmanager ()
. Getdefaultdisplay ();
Intscreenh = Defaultdisplay.getheight ();
Intscreenw = Defaultdisplay.getwidth ();
/* If for landscape * *
if (Intscreenw > Intscreenh)
{
/* Landscape => Portrait * *
Setrequestedorientation (activityinfo.screen_orientation_portrait);
} else
{
/* Portrait => Landscape * *
Setrequestedorientation (Activityinfo.screen_orientation_landscape);
}

The method of overloading onconfigurationchanged (Configuration newconfig) in the activity's Java file is also overloaded so that it does not overload oncreate when layout switching or window switching. The code is as follows:
Java code
Copy Code code as follows:

@Override
public void onconfigurationchanged (Configuration newconfig)
{
super.onconfigurationchanged (newconfig);
if (this.getresources (). GetConfiguration (). Orientation = = Configuration.orientation_landscape)
{
//land
}
Else if (this.getresources (). GetConfiguration (). Orientation = = configuration.orientation_portrait)
{
//port
}
}
@Override
public void onconfigurationchanged (Configuration newconfig)
{
super.onconfigurationchanged (Newconfig);
if (this.getresources (). GetConfiguration (). Orientation = = Configuration.orientation_landscape)
{
//land
}
Else if (this.getresources (). GetConfiguration (). Orientation = = configuration.orientation_portrait)
{
//port
}
}
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.