Android screen Switching

Source: Internet
Author: User
In the Android simulator, the shortcut key "Ctrl + F11/F12" can be used to screen a very simple Activity: package cn.sohu.com; import android. app. activity; import android. OS. bundle; import android. util. log; public class TestActivityActivity extends Activity {@ Override public void onCreate (Bundle savedInstanceState) {Log. I ("TAG", "onCreate () method called"); super. onCreate (savedInstanceState); setContentView (R. layout. main) ;}@ Overrideprotected void onStart () {Log. I ("TAG", "onStart () method called"); super. onStart () ;}@ Overrideprotected void onRestart () {Log. I ("TAG", "onRestart () method called"); super. onRestart () ;}@ Overrideprotected void onResume () {Log. I ("TAG", "onResume () method called"); super. onResume () ;}@ Overrideprotected void onSaveInstanceState (Bundle outState) {Log. I ("TAG", "onSaveInstanceState () method called"); super. onSaveInstanceState (outState) ;}@ Overrideprotected void onRestoreInstanceState (Bundle savedInstanceState) {Log. I ("TAG", "onRestoreInstanceState () method called"); super. onRestoreInstanceState (savedInstanceState) ;}@ Overrideprotected void onPause () {Log. I ("TAG", "onPause () method called"); super. onPause () ;}@ Overrideprotected void onStop () {Log. I ("TAG", "onStop () method called"); super. onStop () ;}@ Overrideprotected void onDestroy () {Log. I ("TAG", "onDestroy () method called"); super. onDestroy ();}}

When switching the screen, we call the following methods in sequence:
05-10 12:01:16. 831: I/TAG (304): onSaveInstanceState () method called
05-10 12:01:16. 831: I/TAG (304): The onPause () method is called.
05-10 12:01:16. 831: I/TAG (304): The onStop () method is called.
05-10 12:01:16. 831: I/TAG (304): onDestroy () method called
05-10 12:01:16. 850: I/TAG (304): The onCreate () method is called.
05-10 12:01:16. 931: I/TAG (304): The onStart () method is called.
05-10 12:01:16. 931: I/TAG (304): onRestoreInstanceState () method called
05-10 12:01:16. 931: I/TAG (304): The onResume () method is called.
It can be seen that the Activity is protected and then destroyed during screen switching. After switching, the Activity is rebuilt and then restored.

We now require that you do not destroy or recreate the Activity during screen switching.
Step 1: add the property android: configChanges = "orientation | keyboardHidden" in AndroidManifest. xml"
Step 2: override the life cycle method onConfigurationChanged (Configuration newConfig) of the activity:
Public void onConfigurationChanged (Configuration newConfig ){
Super. onConfigurationChanged (newConfig );
If (newConfig. orientation = Configuration. ORIENTATION_LANDSCAPE ){
TextView. setText ("Landscape screen now ");
} Else {
TextView. setText ("portrait screen now ");
}
}

In this way, we can set different layout files for different directions on the screen !!!!!!!!!
That is, the layout-land and layout-port directories are created under the res directory. The corresponding layout files remain unchanged and the main. xml is copied to the two directories.
Layout-land is the layout of the horizontal screen, and layout-port is the layout of the vertical screen.

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.