The use of orientation attribute in activity to explain and problems needing attention

Source: Internet
Author: User

do not accumulate kuibu, not even thousands of miles, no small stream, no to become Jianghai. Steed Leap, can not ten steps, Nag travels ten drive, work in not shed . ---------Xunzi's " an exhortation article "

Today, I met a question about orientation. Check a bit of information to record, only a bit of accumulation, to make us stronger, if there is falsehood welcome to criticize

On Android By default, when the "screen orientation" or "keyboard Show 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 Activity:<activity android:name= "in Androidmanifest.xml. Mainactivity "android:configchanges=" keyboardhidden|orientation "> This will not destroy the rebuild after configuring this property, Android:configchanges The Onconfigurationchanged property captures the "screen orientation" and "keyboard display hidden" changes, which are called after the activity's method of () is invoked when these changes are captured.

when I debug on my phone, I find that the onconfigurationchanged () method is not executed, but the activity performs the process of destroying and rebuilding. This is because the version of the problem above the configuration only before the ANDROID4.O version of the function, on Android 4.0 does not work, must be added screensize, that is, Android 4.0 later versions must be configured like this android:configchanges= "keyboardhidden|orientation|screensize" Here we combine examples to say orientation and onconfigurationchanaged methodsCode:
Package Com.example.activitypractice;import Android.app.activity;import Android.content.intent;import Android.content.res.configuration;import Android.os.bundle;import Android.util.log;import Android.view.View; Import Android.view.view.onclicklistener;import Android.widget.button;public class Mainactivity extends Activity { private static final String TAG = "mainactivity";//activity called @overrideprotected void OnCreate when created (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); LOG.I (TAG, "executed the OnCreate method");} The activity was called @Overrideprotected Void OnStart () {Super.onstart () when it was created or returned to the foreground from the background; LOG.I (TAG, "executed the OnStart method");} @overrideprotected void Onrestart () {Super.onrestart () was called when activity returned to the foreground from the background; LOG.I (TAG, "executed the Onrestart method");} @overrideprotected void Onresume () {Super.onresume () is called when activity is created or from being overwritten and back to the foreground in the background; LOG.I (TAG, "executed the Onresume method");} @Overrideprotected void OnPause () {Super.onpause () is called when activity is overwritten with the following or lock screen; LOG.I (TAG, "executed the OnPause method");} Exit the current activity or jump to a newActivity is called @Overrideprotected void OnStop () {super.onstop (); LOG.I (TAG, "executed the OnStop method");} Called when exiting the current activity, the activity ends after the call @Overrideprotected void OnDestroy () {Super.ondestroy (); LOG.I (TAG, "executed the OnDestroy method");} When android:configchanges= "Orientation" is specified, onconfigurationchanged is called when the direction changes, and the activity no longer destroys rebuilding @overridepublic void Onconfigurationchanged (Configuration newconfig) {super.onconfigurationchanged (newconfig); switch ( newconfig.orientation) {Case configuration.orientation_portrait://vertical screen log.i (TAG, "vertical screen"); Setcontentview ( r.layout.portrait); break;case configuration.orientation_landscape://horizontal Screen log.i (TAG, "horizontal screen"); Setcontentview ( R.LAYOUT.LANDSCAP);d Efault:break;}}}
When we are not configuredandroid:configchanges when switching between horizontal and vertical screens ( Note: To set the auto-rotate screen in Settings I use the Samsung test machine that is in the settings--My device--Displays the "auto-rotate screen" option in "Display" ) log as follows:
from the log above you can see that the activity performed the destruction of the rebuild process,onconfigurationchanaged is not called If we 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

The use of orientation attribute in activity to explain and problems needing attention

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.