Thank you !!!
Reprinted Please note: http://blog.csdn.net/richway2010/archive/2011/06/29/6574987.aspx
[Bloggers: Dear bloggers, netizens, and everyone else! Welcome to this blog. You are welcome to have more exchanges, give more comments, learn from each other, and make mutual progress. Our slogan is: study hard and make progress every day .]
I have read many articles about the activity lifecycle. Most of them only illustrate the workflow of a legend, but I have not explained the principles clearly. I will add my personal understanding and analyze its lifecycle.
Like applications on other mobile platforms, the lifecycle of Android applications is under unified control, that is, the fate of the applications we write is in the hands of others (systems, we can't change it. We can only learn and adapt to it.
Simply put, Why is it like this: When a mobile phone is running an application, it is possible to call in and send a text message, or if there is no power, the program will be interrupted at this time, give priority to the basic functions of the service phone. In addition, the system does not allow you to occupy too many resources. At least ensure the phone function. Therefore, when resources are insufficient, the phone may be killed.
Figure:
Analysis:
1. Method for starting activity execution: oncreate () --> onstart () --> onresume () method. At this time, the program processes the running status.
Method principle:
Oncreate: Create an interface to initialize data.
Onstart: In this step, the user can see and cannot interact with each other.
Onresume: it becomes interactive with the user. (The activity stack system manages the top of these activities through stacks. After the stack is run, it returns to the previous activity)
2. The method for terminating the activity is onpause () --> onstop () --> ondestory (). At this time, the program is destroyed.
Method principle:
Onpause: This step is visible but interactive, and the system will stop animation and other CPU-consuming tasks. As you can see from the above description, you should save some of your data here, at this time, your program has a lower priority and may be reclaimed by the system. The data stored here should be read out in onresume. Note: The time for doing this method is short, because the next activity will not start until this method is complete.
Onstop: becomes invisible and overwritten by the next activity
Ondestroy: This is the last method called before the activity is killed. It may be that the external class calls the finish method or the system temporarily kills the method to save space. You can use isfinishing () to judge it. If you have a progress dialog online task, please drop it cancel in ondestroy. Otherwise, when the thread ends, calling the Cancel Method of dialog will throw an exception.
3. onpause () --> onresume conversion Principle
The activity can be switched between the resumed and paused statuses frequently. When the activity processes the running status, a new activity runs to the foreground, and the original activity is pushed to the stack, the current activity is at the top of the stack.
4. Idle activity for a long time --> to the running state
Onstop () --> onrestart () --> onstart () --> onresume () --> activity running
Note: In onpause, onstop, ondestroy, the activity may be killed by the system because other applications need to use memory. Other statuses will not end.