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 androidproject in Eclipse. Named Example4_1, change the contents of its Mainactivity class as seen in example 4-1.
"Example 4-1activity life cycle Demo"
01//Omit a number of import files here, by Eclipse's own initiative to generate the public classmainactivity extends Activity { Class mainactivity inherits class Activity03 String tag= "Activity life cycle Event"; @Override05 Prot ected voidoncreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 07 Setcontentview (R.layout.activity_main); LOG.E (TAG, "Start OnCreate event"); 09}10 @Override11 protected Voidondestroy () { Rewrite OnDestroy events//TODO Auto-generatedmethod stub13 Super.ondestroy (); LOG.E (TAG, "Start OnDestroy event"); Show records in Logcat}16 @Override17 protected void OnPause () {18 TODO Auto-generatedmethod Stub19 Super.onpause (), LOG.E (TAG, "Start OnPause event");}22 @Override23 Protected Voidonrestart () {//TODO Auto-generatedmethod STUB25 Super.onrestart (); LOG.E (TAG, "Start Onrestart event");}28 @Override29 protected void Onresume () {//TODO Auto-generatedmethod stub31 Supe R.onresume (), LOG.E (TAG, "Start Onresume event"),}34 @Override35 protected void OnStart () {//TODO Auto-generatedmethod stub37 Super.onsta RT (), LOG.E (TAG, "Start OnStart event"),}40 @Override41 protecte d void OnStop () {auto-generatedmethod//TODO stub43 super.onstop (); 44 Log. E (TAG, "Start OnStop event"); 45}46}
After execution, you can see that 4-2 of what you see is displayed in the Logcat form.
Tip: You can filter the information in the Logcat by setting a filter so that it shows only the records associated with the activity, as seen in 4-3. Filtered content 4-4 is seen.
Go back and look at a section of the description of the start activity, to start an activity need to experience OnCreate, OnStart, Onresume 3 events. This can be confirmed in Figure 4-4.
The ability to verify the contents of the previous section by further operation of the phone, such as clicking the Home button or the Back button to observe the Logcat in the log.
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvym9va3poyw9waw4=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">
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 Form
Right-click in the code editing area of Eclipse. Select source| The Overridemethods command can see some other events, as seen in 4-5.
Figure 4-5 Some other events in the activity
Most of these events correspond to a particular operation or error that is applied. For example, event ontitlechanged is the event that is emitted by the system when the title of the app is altered; Music played to 1 minutes and 50 seconds when suddenly a call came in. The system will use the OnPause method for player activity. 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, including more than 20 app instances and 3 app projects, but also combined with HTML5 mobile development.
This book is for mobile development starter, low threshold. Shallow reading, the most suitable for mobile app startup small white. The 3rd to 5th chapter of this publication is the basis for readers ' learning. Interested in Welcome to buy this book! Welcome Dabigatran:348632872, ask the author to communicate directly.
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvym9va3poyw9waw4=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center "style=" border:none; max-width:100%; Color:rgb (51,51,51); font-family:arial; font-size:14px; Line-height:26px ">
Small white Learn PhoneGap "build cross-platform App:phonegap mobile app" serial Three (experience life cycle through an instance)