Android Program Development: (7) process changes-7.1 trigger events

Source: Internet
Author: User

Currently, we have seen two ways to adapt to changes in the screen direction. Now let's take a look at the changes in the Activity status when the screen direction changes.

1. Create a project: Orientations.


2. main. xml file.

[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
<! -- Note that this EditText uses the android: id attribute -->
<EditText
Android: id = "@ + id/txtField1"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>
<! -- Note that this EditText does not use the android: id attribute -->
<EditText
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>
 
</LinearLayout>
3. OrentationsActivity. java.
[Java]
Public class OrientationsActivity extends Activity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Log. d ("StateInfo", "onCreate ");
}

@ Override
Public void onStart (){
Log. d ("StateInfo", "onStart ");
Super. onStart ();
}
 
@ Override
Public void onResume (){
Log. d ("StateInfo", "onResume ");
Super. onResume ();
}
 
@ Override
Public void onPause (){
Log. d ("StateInfo", "onPause ");
Super. onPause ();
}
 
@ Override
Public void onStop (){
Log. d ("StateInfo", "onStop ");
Super. onStop ();
}
 
@ Override
Public void onDestroy (){
Log. d ("StateInfo", "onDestroy ");
Super. onDestroy ();
}
 
@ Override
Public void onRestart (){
Log. d ("StateInfo", "onRestart ");
Super. onRestart ();
}
 
}
4. debugging.
5. Enter some characters in two edittexts. Just like this.

 

6. Press Ctrl + F11 to change the screen direction. The simulator on the horizontal screen is displayed. Note that the content of the first EditText still exists, but the second EditText has been cleared. (The end of the article explains why)

 

7. Observe the LogCat window.

[Html]
12-15 12:27:20. 747: D/StateInfo (557): onCreate
12-15 12:27:20. 747: D/StateInfo (557): onStart
12-15 12:27:20. 747: D/StateInfo (557): onResume
...
12-15 12:39:37. 846: D/StateInfo (557): onPause
12-15 12:39:37. 846: D/StateInfo (557): onStop
12-15 12:39:37. 866: D/StateInfo (557): onDestroy
12-15 12:39:38. 206: D/StateInfo (557): onCreate
12-15 12:39:38. 216: D/StateInfo (557): onStart
12-15 12:39:38. 257: D/StateInfo (557): onResume
The LogCat window shows that the Activity is destroyed when the screen direction changes.
[Html]
12-15 12:39:37. 846: D/StateInfo (557): onPause
12-15 12:39:37. 846: D/StateInfo (557): onStop
12-15 12:39:37. 866: D/StateInfo (557): onDestroy
Then it is created again.
[Html]
12-15 12:39:38. 206: D/StateInfo (557): onCreate
12-15 12:39:38. 216: D/StateInfo (557): onStart
12-15 12:39:38. 257: D/StateInfo (557): onResume
Understanding this kind of Activity behavior change is very important, because before the screen direction changes, we need to perform some necessary operations to save the "status" of the current activity ". For example, your activity has some variables that save some values that need to be calculated and these values need to be saved. For any activity, you should save the "status" in the onPause () method, because the onPause () method is the first method that appears when the screen direction changes. The following tutorials will illustrate several different ways to save the "status.

!!!

Another note: when the activity for storing views is destroyed, only the views declared in the activity (by using the android: id attribute) will save their own States. For example, when a user inputs information in an EditText file, the user may change the screen direction. When the activity is re-created, the information in EditText is automatically saved and re-displayed in EditText. If you do not use android: id to declare EditText, the activity will not save any content of EditText.

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.