4.1.2 Experience the Activity life cycle by example

The previous section covered the processes in the Activity life cycle, and this section gives you a hands-on experience with the events in the Activity life cycle with a simple example.

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 Lifecycle Demonstration

01 // Omit several import files here, 02 public classMainActivity extends Activity {public classMainActivity extends Activity 03 String Tag =”Activity Lifetime Event “; 04 @Override 05 protected voidonCreate(Bundle savedInstanceState) { 06 super.onCreate(savedInstanceState); 07 setContentView(R.layout.activity_main); 08 log. e(TAG,” start onCreate event “); 9} 10 @Override 11 protected VoidOnDestroy () {// Override the OnDestroy event 12 // Todo Auto-GeneratedMethod stub 13 super.onDestroy(); 14 log. e(TAG,” Start the OnDestroy event “); } 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 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 “); 46 45}}

After you run it, you see something like Figure 4-2 displayed in the LogCat window.

Tip: You can set a filter to filter the information in LogCat so that it displays only the records relevant to the Activity, as shown in Figure 4-3. The filtered content is shown in Figure 4-4.

Going back to the description of starting an Activity in the previous section, starting an Activity requires going through three events, onCreate, onStart, and onResume, as you can see in Figure 4-4. You can verify the content of the previous section by doing further actions on your phone, such as clicking the Home or Back button to see the logging in LogCat.



Figure 4-2 Events that your Activity experiences when it starts

Figure 4-3 uses the Tag Tag to filter the information in LogCat



Figure 4-4 Filtered LogCat window

Right-click in the Eclipse code editing area, select the Source | Overridemethods command can see some other events, as shown in figure 4 to 5.



Figure 4-5 Some other events in the Activity

Most of these events correspond to a specific action or error in the application. For example, the event OnTitleChanged is an event emitted by the system when the title of the application is changed. For example, when listening to music, a phone call comes in at 1:50, the system will use onPause on the player Activity and start an Activity to answer the phone. When the user finishes answering the phone, it will return to the music playback interface via onRestar and onStar methods. And adjust the music playing progress to 1 minute 50 seconds.

Tsinghua University Press ‘Building Cross-Platform Apps: PhoneGap Mobile Apps in Action’ is an APP practice book, containing more than 20 APP examples and three APP projects, and also combined with HTML5 mobile development. This book for mobile development beginners, low threshold, shallow reading, the most suitable for mobile APP entrepreneurship Xiaobai. Chapters 3~5 of this release serve as the basis for readers to learn, and those who are interested are welcome to buy this book! Welcome to add group: 348632872, direct questions to the author exchange!