Life cycle of Android activity

Source: Internet
Author: User

Mastering the life cycle of an activity is important to any Android developer, and when you understand the life cycle of an activity, you can write more consistent and streamlined programs.

-----------------------------------------------return Stack-----------------------------------------------------------

Android activities can be stacked. Each launch of a new activity will overwrite the previous activity and then click the back key to destroy the top activity, and one of the following activities will be displayed again.

In fact, Android is a task to manage activities, a task is a set of activities stored in the stack, this stack is also known as the return stack (back stack).

By default, whenever we start a new activity, it is stacked in the back stack and is positioned at the top of the stack. And whenever we press the back key or call the finish () method to destroy an activity, the activity on top of the stack will be out of the stack, then the previous stack of activities will be back to the top of the stack position.

The system always displays the activity at the top of the stack to the user.

-----------------------------------------------Activity Status------------------------------------------------

running state: When an activity is at the top of the stack at the back of the stack, the activity is in the running state.

paused state: When an activity is no longer in the top position of the stack, but is still visible, the activity enters a paused state. The system will not consider recycling this activity at very low memory.

Stop state: When an activity is completely invisible, it enters a stop state. The system will still save the appropriate state and member variables for this activity, but it is not reliable and may still be reclaimed by the system.

destroy state: When an activity is removed from the return stack, it becomes destroyed. The system reclaims activity in this state to ensure that the phone has sufficient memory.

---------------------------------------------the lifetime of the activity------------------------------------------------

The activity class has seven callback methods that cover every aspect of the active life cycle.

1.onCreate ()———————— initialization operation, called when the activity is first created

2.OnStart ()————————— start operation, call when activity becomes visible from invisible

3.onresume ()———————— Interactive operation, called when the activity is ready to interact with the user

4.onPause ()————————— suspend operation, called when the system is ready to start or resume another activity

5.onStop ()—————————— stop operation, call when activity is completely invisible

6.OnDestroy ()———————— recycle operation, call before activity is destroyed

7.Onrestart ()———————— Restart operation, called before the activity is stopped into a running state

The above method, in addition to the Onrestart () method, others are corresponding, so that the activity can be divided into three life cycle

Full lifetime:

  The active OnCreate () method and the OnDestroy () method are the complete life cycle. In general, an activity completes various initialization operations in the OnCreate () method, while freeing the memory in the OnDestroy () method.

Visible Lifetime:

The activity that is experienced between the OnStart () method and the OnStop () method is the visible life cycle. During the visible lifetime, activities are always visible to the user, even though they may not be able to interact with the user. These two methods can be used to reasonably manage the resources that are visible to the user.

Foreground lifetime:

The activity is experienced between the Onresume () method and the OnPause () method, which is the foreground life cycle. In the foreground lifetime, the activity is always in the running state, at this time the activity is can interact with the user, and the user most contact is the activity in this state.

  

Original address: http://www.cnblogs.com/woider/p/5117034.html

Life cycle 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.