The lifetime of Android activity

Source: Internet
Author: User

There are seven callback methods defined in the activity class that cover every aspect of the active life cycle, and here I come to one by one
The following seven methods are described.
1. OnCreate ()
This method you have seen many times, each activity we have rewritten this method, it will be in the activity
Called the first time it is created. You should complete the initialization of the activity in this method, such as loading the cloth
Office, 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 time must be on the return stack
Top of the stack, 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 do this on the side
Frees up some CPU-consuming resources and saves some key data, but the speed at which the method executes
Must be fast, or it will affect the use of new top-of-stack activities.
5. OnStop ()
This method is called when the activity is completely invisible. The main difference between it and the OnPause () method is that, for example,
The new activity that starts is a dialog-style activity, then the OnPause () method is executed, while the OnStop ()
method does 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.
The above seven methods except the Onrestart () method, the other is 22 relative, so that the activity can be divided into three
Survival period.

1. Full lifetime
The activity between the OnCreate () method and the OnDestroy () method is the complete survival period. General situation
, an activity completes various initialization operations in the OnCreate () method, while the OnDestroy () method finishes
into a free memory operation.
2. Visible lifetime
The activity that is experienced between the OnStart () method and the OnStop () method is the visible lifetime. In the visible survival
Period, activities are always visible to the user, even though they may not be able to interact with the user. We can pass these two
Methods to manage resources that are visible to the user in a reasonable way. For example, to load a resource in the OnStart () method,
The resource is freed in the OnStop () method to ensure that activities that are in a stopped state do not consume too much memory.
3. Foreground lifetime
The activity between the Onresume () method and the OnPause () method is the foreground lifetime. At the front desk
During the lifetime, the activity is always in the running state, at this time the activity is can and the user mutual, we usually
See and touch most of the activities in this State as well.

The lifetime of Android activity

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.