Android:activity life cycle

Source: Internet
Author: User

Android uses tasks to manage activities, and a task is a set of activities that are stored in the stack, also known as the back stack.

The stack is a last-in-first-out data structure that, by default, is placed in the stack on the back stack and at the top of the stack whenever we start a new activity.

Whenever we press the back key or call the finish () method to destroy an activity, the activity at the top of the stack will go out of the stack, and the previous stack activity will be re-positioned at the top of the stack. The system always displays the activity at the top of the stack to the user.

I. Status of ACTIVITIES
Each activity may have a maximum of four states in its life cycle.
1. Operational status
When an activity is at the top of the stack at the back of the stack, the activity is in the running state. The last thing the system wants to recycle is a running activity, because it brings a very poor user experience.
2. Pause Status
When an activity is no longer in the top position of the stack, but is still visible, the activity enters a paused state. You may think that since the activity is not at the top of the stack, how can it be visible? This is because not every activity will fill the entire screen,

For example, a dialog-like activity takes up only a portion of the middle of the screen, and you'll soon see this activity later. The suspended activity is still completely alive and the system is reluctant to recycle it.

(because it's still visible, the things that recycle can have a bad impact on the user experience), the system will consider recycling this activity only if the memory is very low.
3. Stop state
When an activity is no longer at the top of the stack and is completely invisible, it enters a stop state. The system will still save the appropriate state and member variables for this activity, but this is not completely reliable, and when memory is needed elsewhere, the activity in the stopped state may be reclaimed by the system.
4. Destruction Status
When an activity is removed from the return stack, it becomes a destroyed state. The system will most likely recycle activities in this State, thus ensuring that the phone has sufficient memory.

Second, the life cycle of activities
The activity class defines seven callback methods that cover every aspect of the active life cycle, and the following seven methods are described below.
1. OnCreate ()
You have seen this method many times, and we have rewritten this method in each activity, and it will be called when the activity is first created. You should perform initialization of activities in this method, such as loading layouts, binding events, and so on.
2. OnStart ()
This method is called when the activity becomes visible from invisible.
3. Onresume ()
This method is called when the activity is ready to interact with the user. The activity at this point must be at the top of the stack that is returned and is in a running state.
4. OnPause ()
This method is called when the system is ready to start or resume another activity. We usually release some CPU-consuming resources in this method and save some key data, but this method must be executed quickly, otherwise it will affect the use of the new stack top activity.
5. OnStop ()
This method is called when the activity is completely invisible. The main difference between it and the OnPause () method is that if the new activity started is a dialog-box activity, then the OnPause () method will be executed, and the OnStop () method will not execute.
6. OnDestroy ()
This method is called before the activity is destroyed, and then the state of the activity becomes the destroy state.
7. Onrestart ()
This method is called before the activity is changed from the stopped state to the running state, i.e. the activity is restarted.

Android officially provides an active life cycle of


In addition to the Onrestart () method in the above seven methods, the others are 22 relative, and the activity can be divided into three lifetimes.
1. Full lifetime
The activity between the OnCreate () method and the OnDestroy () method is the complete survival period. In general, an activity completes various initialization operations in the OnCreate () method, while freeing the memory in the OnDestroy () method.
2. Visible lifetime
The activity that is experienced between the OnStart () method and the OnStop () method is the visible lifetime. During the visible lifetime, activities are always visible to the user, even though they may not be able to interact with the user.

Through these two methods, we can reasonably manage the resources that are visible to the user. For example, the resource is loaded in the OnStart () method, and the resource is freed in the OnStop () method to ensure that the inactive activity does not consume too much memory.
3. Foreground lifetime
The activity between the Onresume () method and the OnPause () method is the foreground lifetime. In the foreground life period, the activity is always in the running state, at this time the activity is can and the user to each other, we usually see and contact the most also this state of activity.


Android:activity life cycle

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.