Android-life cycle of an activity

Source: Internet
Author: User

Back to Stack

Active status---Four active states
    • The running state is the state that the application renders to the main interface before us;
    • The pause state pause state is somewhat similar to the third stop state, but the paused state is a type of active state that you can also see part of the interface, such as typing keyboards that send message time calls. The relative stop state is completely not able to be seen by us;
    • Stop state This state is usually returned to the top of the stack at the time of the call, appearing in our view;
    • The destruction state system takes the highest priority to reclaim the memory occupied by activities that destroy State;

  Possibility of the system reclaiming memory in four states destroy State > Stop state > Pause State > Running state where the running state and the suspended state are recycled very little;

The lifetime of the activity----seven methods in addition to the Onrestart () method, the others are 22 relative, and the activity can be divided into three lifetimes.

1. OnCreate (): This method you have seen many times, each activity we have rewritten this method, it will be the first time the activity was created when the call. 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 and the user interacts. 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 stop state to the running state, i.e. the activity is restarted.

Three life cycle-----full life cycle visible lifetime foreground life cycle

    1. Full life cycle

      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, and the release memory is done in the OnDestroy () method.

    2. Visible Life Time

      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 . 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. 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 to each other, we usually see and contact most of the activities in this state.

Android-life cycle of an 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.