What happens when the Android screen is switched vertically

Source: Internet
Author: User

Screen orientation changes occur in many applications, but what happens when things change?


Let's write an app to test it.

Here's the code: http://download.csdn.net/detail/edwardwayne/8594291


We wrote a few important stages of the Android life cycle oncreate,onresume,onstart,ondestory,onstop approach in mainactivity


Calling the Toast method in it tells us that there are no triggers at these stages.


In addition we rewrite onsaveinstancestate () and onrestoreinstancestate ()


In them, as well. Add Toast Hints


This time we enter the app, rotating the phone we will find in turn OnStop,onsaveinstancestate, Ondestroy,oncreate,onstart, Onrestoreinstancestate, Onresume


We can understand that by default, changing the direction app will cycle through the life cycle, which in the test will be easier to cause the app to crash.


In order to avoid this situation, we can join in the activity of manifest

android:configChanges="orientation|screenSize|keyboardHidden"
And we joined in the mainactivity.

public void onconfigurationchanged (Configuration newconfig) {//TODO auto-generated method Stubsuper.onconfigurationchanged (Newconfig); if (newconfig.orientation = = Configuration.orientation_landscape) { Toast.maketext (This, "landscape", Toast.length_short). Show (); else if (newconfig.orientation = = configuration.orientation_portrait) {Toast.maketext (this, "PORTRAIT", toast.length_ Short). Show ();}}

This time we change the direction of the phone, we will be the birth, the app has no cycle life cycle, but triggered the onconfigurationchanged method.

As for the function of Keyboardhidden is to hide the keyboard after rotating the mobile phone direction.


What happens when the Android screen is switched vertically

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.