Android Screen Toggle

Source: Internet
Author: User
<span id="Label3"></p><p><p>Fix Android phone screen to switch vertically</p></p><p><p>When the screen is switched on and off in android, the Activity's life cycle is reloaded (indicating that the current activity is destroyed, but then reloaded), how can the current activity not be destroyed when the screen is switched on or off?</p></p><p><p><strong><span style="color: #008000;">1. Set the Configchanges property for activity in androidmanifest.xml,</span></strong></p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><span style="color: #000000;">Application Android:icon<span style="color: #000000;">=<span style="color: #000000;">"<span style="color: #000000;">@drawable/icon<span style="color: #000000;">"<span style="color: #000000;">Android:label<span style="color: #000000;">=<span style="color: #000000;">"<span style="color: #000000;">@string/app_name<span style="color: #000000;">"<span style="color: #000000;">><span style="color: #000000;"> <span style="color: #000000;"><<span style="color: #000000;">Activity Android:name<span style="color: #000000;">=<span style="color: #000000;">"<span style="color: #000000;">. Mainactivity<span style="color: #000000;">"<span style="color: #000000;">Android:label<span style="color: #000000;">=<span style="color: #000000;">"<span style="color: #000000;">@string/app_name<span style="color: #000000;">"<span style="color: #ff0000;"><strong><strong>android:configchanges= "orientation|keyboardhidden"</strong></strong><span style="color: #000000;">><span style="color: #000000;"> <span style="color: #000000;"><<span style="color: #000000;">Intent<span style="color: #000000;">-<span style="color: #000000;">Filter<span style="color: #000000;">><span style="color: #000000;"> <span style="color: #000000;"><<span style="color: #000000;">Action Android:name<span style="color: #000000;">=<span style="color: #000000;">"<span style="color: #000000;">Android.intent.action.MAIN<span style="color: #000000;">"<span style="color: #000000;"> <span style="color: #000000;">/><span style="color: #000000;"><span style="color: #000000;"> <span style="color: #000000;"><<span style="color: #000000;">category android: Name<span style="color: #000000;">=<span style=" color: #000000; " "<span> Android.intent.category.launcher<span style="color: #000000;"> "<span style=" color: #000000; "> <span style=" Color : #000000; ">/><span style="color: #000000;"> <span style="color: #000000;"></<span style="color: #000000;">intent< Span style= "color: #000000;" >-<span style="color: #000000;">filter<span style="color: #000000;">><span style="color: #000000;"> <span style="color: #000000;"></<span style="color: #000000;">activity<span style="color: #000000;">><span style="color: #000000;" span></<span style="color: #000000;">application<span style="color: #000000;">> </span> </span> </span> </span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p><strong><span style="color: #008000;">Configchanges has the following options</span></strong> : 1. Orientation: the screen rotates between portrait and landscape, 2.  Keyboardhidden: keyboard display or hide, 3.fontScale: user changed the preferred font size 4.locale: user selected different language settings, 5. Keyboard: keyboard type changes, such as phone switching from 12 keyboard to full keyboard 6. Touchscreen or navigation: changes in keyboard or navigation mode,</p></p><p><p>If the absence of the Keyboardhidden option does not prevent the destruction of the activity, and events in the later mentioned Onconfigurationchanged event can only capture the vertical screen, the horizontal screen will not capture the vertical screen <strong> <span style="color: #008000;">2. Rewrite in the corresponding activity: onconfigurationchanged method:</span></strong></p></p><p><p></p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><span style="color: #0000ff;">Public<span style="color: #000000;"> <span style="color: #0000ff;">Class<span style="color: #000000;">Mainactivity<span style="color: #0000ff;">Extends<span style="color: #000000;">Activity {<span style="color: #0000ff;">Private<span style="color: #000000;">TextView TextView; @Override<span style="color: #0000ff;">Public<span style="color: #000000;"> <span style="color: #0000ff;">void<span style="color: #000000;">OnCreate (Bundle Savedinstancestate) {<span style="color: #0000ff;">Super<span style="color: #000000;">. onCreate (savedinstancestate); Setcontentview (r.layout.main); LOG.I (<span style="color: #000000;">"<span style="color: #000000;">--main--<span style="color: #000000;">"<span style="color: #000000;">,<span style="color: #000000;">"<span style="color: #000000;">OnCreate<span style="color: #000000;">"<span style="color: #000000;">); TextView<span style="color: #000000;">=<span style="color: #000000;">(TextView) Findviewbyid (r.id.tv_id); } @Override<span style="color: #0000ff;">Public<span style="color: #000000;"> <span style="color: #0000ff;">void<span style="color: #000000;">Onconfigurationchanged (Configuration Newconfig) {<span style="color: #0000ff;">Super<span style="color: #000000;">. onconfigurationchanged (newconfig); LOG.I (<span style="color: #000000;"><span style="color: #000000;"> "<span style=" color: #000000; ">--main--<span style=" color: #000000; ">" <span style="color: #000000;">, <span style="color: #000000;"> "<span style=" color: #000000; ">onconfigurationchanged<span style=" color: #000000; " "<span>); <span style="color: #0000ff;">if<span style="color: #000000;"> (newconfig.orientation <span style="color: #000000;">==<span style="color: #000000;">configuration.orientation_landscape) { Textview.settext (<span style="color: #000000;"> "<span style=" color: #000000; "> The current screen is a horizontal screen <span style="color: #000000;"> "<span style=" color: #000000; ">);} <span style="color: #0000ff;">else<span style="color: #000000;">{textview.settext ( <span style="color: #000000;"> "<span style=" color: #000000; "> Current screen is vertical <span style=" color: #000000; ">" <span style="color: #000000;">); } } }</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p></p></p><p><p></p></p><p><p></p></p><p><p>The layout file is a simple TextView not given here,</p></p><p><p>The effect is as Follows:</p></p><p><p>Log printing:</p></p><p><p></p></p><p><p>From the log can be analyzed in the screen to switch the activity is not destroyed, of course, you can also run the project in the OnCreate method to make a breakpoint, the execution of the discovery OnCreate method is only in the beginning of the execution, the screen at the same time switch, is not in the execution, So you can do this in the onconfigurationchanged method.</p></p><p><p>Note: If the item does not require a screen switch, it can be set to</p></p><p><p>1. android:screenorientation= "portrait" always displays 2 on vertical Screen. Android:screenorientation= "landscape" is always displayed as a horizontal screen</p></p><p><p><strong><span style="color: #0000ff;">Above the configuration file settings screen, The following is the code to control the screen in the Portrait:</span></strong></p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><span style="color: #0000ff;">Private<span style="color: #000000;">Onclicklistener OnClick<span style="color: #000000;">=<span style="color: #0000ff;">New<span style="color: #000000;">Onclicklistener () {@Override<span style="color: #0000ff;">Public<span style="color: #000000;"> <span style="color: #0000ff;">void<span style="color: #000000;">OnClick (View V) {<span style="color: #008000;">//<span style="color: #008000;">Set screen to horizontal screen<span style="color: #008000;"> <span style="color: #000000;"> <span style="color: #0000ff;">If<span style="color: #000000;">(v<span style="color: #000000;">==<span style="color: #000000;">Butlandscrpe) {mainactivity.<span style="color: #0000ff;">This<span style="color: #000000;">. setrequestedorientation (activityinfo.screen_orientation_landscape);<span style="color: #008000;">//<span style="color: #008000;">Set the screen as vertical screen<span style="color: #008000;"> <span style="color: #000000;">}<span style="color: #0000ff;">Else<span style="color: #000000;">{mainactivity.<span style="color: #0000ff;">This<span style="color: #000000;">. setrequestedorientation (activityinfo.screen_orientation_portrait); } } };<span style="color: #008000;">//<span style="color: #008000;">Listen for changes to system settings<span style="color: #008000;"> <span style="color: #000000;">@Override<span style="color: #0000ff;"><span style="color: #0000ff;">public<span style="color: #000000;"> <span style="color: #0000ff;">void<span style="color: #000000;"> onconfigurationchanged (Configuration Newconfig) {<span style="color: #0000ff;">super <span style="color: #000000;">.onconfigurationchanged (newconfig); String message<span style="color: #000000;">=<span style="color: #000000;">newconfig.orientation<span style=" Color: #000000; ">==<span style="color: #000000;">configuration.orientation_landscape <span style="color: #000000;">?<span style="color: #000000;"> <span style=" color: #000000; " "<span> screen set to: landscape <span style=" color: #000000; ">" <span style="color: #000000;">: <span style=" color: #000000; " "<span> screen set to: portrait <span style=" color: #000000; ">" <span style="color: #000000;">; Showtoast (message); }</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>  </p></p><p><p>Friends can go to try, have any comments or suggestions welcome message, thank you!</p></p><p><p>Android Screen Toggle</p></p></span>

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.