4.1.2 Experience the life cycle of an activity by example
The previous section describes the processes in the activity life cycle, and this section takes a simple example to give the reader a firsthand experience of each event in the activity life cycle.
Create a new Android project in Eclipse, named Example4_1, and modify the contents of its Mainactivity class as shown in example 4-1.
"Example 4-1activity life cycle Demo"
01 //A number of import files are omitted here, automatically generated by Eclipse 02 public classMainActivity extends Activity { // Class Mainactivity inheriting class activity 03 string tag= "Activity life cycle Events"; 04 @Override 05 protected voidoncreate (bundle savedinstancestate) { 06 &nbSp; super.oncreate (savedinstancestate); 07 Setcontentview (R.layout.activity_main); 08                LOG.E (TAG, " Start OnCreate event "); 09 } 10 @ override 11 Protected voidondestroy () { //overriding OnDestroy Events 12 // TODO auto-generatedmethod stub 13 super.ondestroy (); 14       LOG.E (TAG, "Start OnDestroy event"); //Show records in Logcat 15 } 16 @Override 17 protected void onpause () { 18 // TODO Auto-generatedmethod stub 19 super.onpause (); 20                LOG.E (TAG, " Start OnPause event "); 21 } 22 @Override 23 protected voidonrestart () { 24 // TODO Auto-generatedmethod stub 25 super.onrestart (); 26                 LOG.E (TAG, " Start Onrestart event "); 27 } 28 @ Override 29 protected void Onresume () { 30 // todo auto-generatedmethod stub 31 super.onresume (); 32  LOG.E (TAG, "Start Onresume event"); 33 } 34 @Override 35 protected&nbsP;void onstart () { 36 // todo auto-generatedmethod stub 37 super.onstart (); 38    LOG.E (TAG, "Start OnStart event"); 39 } 40 @Override 41 protected void onstop () { 42 // todo auto-generatedmethod stub 43 super.onstop (); 44       LOG.E (TAG, "Start OnStop event"); 45 } 46 }
After running, you can see the contents shown in the Logcat window in 4-2.
Tip: You can filter the information in the Logcat by setting a filter so that it displays only records related to that activity, as shown in 4-3. The filtered contents are shown in 4-4.
Look back and look at the description of the start activity in a section, to start an activity you need to go through OnCreate, OnStart, Onresume 3 events, you can confirm this in Figure 4-4. You can verify the contents of the previous section by further manipulating your phone, such as clicking the Home button or the Back button to see the log records in the Logcat.
Figure 4-2 events that were experienced when activity was started
Figure 4-3 Filtering the information in the LOGCAT using tag tags
Figure 4-4 Filtered Logcat window
Right-click in the code editing area of Eclipse and select source| The Overridemethods command can see some other events, as shown in 4-5.
Figure 4-5 Some of the other events in activity
Most of these events correspond to a particular operation or error of the application. For example, event ontitlechanged is the event that is emitted by the system when the title of the app is modified, such as listening to music, music playing to 1 minutes 50 seconds when suddenly call in a phone, the system will use the player's activity OnPause method, At the same time start a call activity, when the user after the call will be returned through the Onrestar method and the OnStar method to return to the music interface, and adjust the music playback progress of 1 minutes 50 seconds.
Tsinghua University Press launched the "build cross-platform App:phonegap mobile app" is an app book, contains more than 20 app instances and 3 app projects, but also combined with HTML5 mobile development. This book is for mobile development beginners, low threshold, shallow reading, the most suitable for mobile app startup small white. This release of the 3rd to 5th chapter as the basis for readers to learn, interested in the Welcome to buy this book! Welcome Dabigatran:348632872, direct questions to the author of the Exchange!
PhoneGap, "Building cross-platform APP:PHONEGAP mobile Applications" serial Three (experience life week through examples)