PhoneGap, "Building cross-platform APP:PHONEGAP mobile Applications" serial Three (experience life cycle through examples)

Source: Internet
Author: User

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//Omit a number of import files here, automatically generated by Eclipse Classmainactivity extends Activity { Class mainactivity inherits class Activity03 String tag= "Activity life cycle Event"; @Override05 Protec                        Ted 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 (); 14                                        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 su              Per.onrestart (); LOG.E (TAG, "Start Onrestart event");}28 @Override29 protected void Onresume () {//TODO Auto-generatedmethod Stub31 Super. Onresume (), LOG.E (TAG, "Start Onresume event"),}34 @Override35 p rotected void OnStart () {//TODO Auto-generatedmethod stub37 Super.onstart (), LOG.E (TAG, "Start OnStart event"),}40 @Override41 protected                        void OnStop () {auto-generatedmethod//TODO stub43 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 cycle through examples)

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.