4.1 What is the life cycle

To truly understand what PhoneGap application development is all about, you first need to understand what a lifecycle is. This is pretty easy to understand on paper. The life cycle between an app being loaded by the phone and an app being quit is called a life cycle. In order to make it easier for readers to understand, this section will use the life cycle of the Activity class in the Android native SDK combined with the LogCat debugging tool in Eclipse to give a practical explanation.

4.1.1 Activity Lifecycle

Take a closer look at Figure 4-1, which is an Activity Lifecycle Flow Chart from Google. It includes the various events an Android application goes through from the time it is created to the time it ends. Below are the processes that go through the Activity lifecycle.

(1) Start the Activity: The system will call the onCreate method to create a new Activity object, and then call the onStart method and the onResume method in turn to make the newly created Activity into running state.

(2) Pause state: the current Activity is overwritten by another Activity or the phone lock screen, the original Activity is placed in the background, the system will call onPause method to enter the paused state of the Activity.

(3) Restore state: When the paused Activity is restarted, the system will call onResume to bring it back to the running state.

(4) Background state: When the user clicks the Home button to return to the Home screen, the Activity is saved in the background, and the system will call onPause and then onStop to pause the Activity.

(5) Return state: When the user starts the Activity again, the system will call the onRestar method first, then the onStar method, and finally the onResume method to return the application to the running state.

(6) The current Activity is in the overwritten state or the background invisible state, that is, step 2 and step 4, the system memory is insufficient, kill the current Activity, and then the user returns to the current Activity: call onCreate method, onStart method, onResume method again, and enter the running state.

(7) The user exits the current Activity: the system calls onPause, then onStop, and finally onDefory to end the current Activity.



Figure 4-1 Activity Lifecycle

After some explanation, the reader should be able to make sense of Figure 4-1, but why?

As we all know, one of the key features of a smartphone over a non-smartphone is that it has a “background” and can run multiple programs at the same time. For example, you can listen to music while browsing the content of a micro blog while hanging on QQ, and when someone calls in, the phone can automatically switch the interface of answering the call, all through the life cycle.

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!