Life cycle Learning for Android

Source: Internet
Author: User

Mastering the Android life cycle is critical to how an Android beginner has just touched, and will leave me with more time in the development process, and will write a smoother program when I'm getting to know the entire statement cycle in Android.

The application will have a better user experience effect

Activity status

There are four states in an active life cycle in Android

1. The running state is also known as the display state: When an activity is in or returns to the top of the stack, the activity is in the running state

2. 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

3. Stop state: When an activity is no longer at the top of the stack and is unavailable, the activity is in a stopped state

4. Destroy state: When an activity is removed from the top of the stack, it enters the destroy state. The system recycles activities in this State to ensure that the phone system has sufficient memory

Activity cycle

The activity cycle diagram is preceded by a picture from the Official document (http://developer.android.com/reference/android/app/Activity.html), which I believe we have seen.

                                                                                                                      
Methoddescription can it be killed later? Next Method
OnCreate () Called when the activity is created for the first time. Here you should do all the usual static setup work-creating a view, binding the list data, and so on. This method passes in a bundle object that contains the previous state of the activity (if it was previously captured, see saving Activity State later).

The next callback method is always OnStart ().

Whether OnStart ()
Onrestart () Called before the activity is stopped and then started again.

The next callback method is always OnStart ()

Whether OnStart ()
OnStart () Called before the activity is to be displayed to the user.

If the activity enters the foreground, the next callback method is Onresume (), or the next callback method is OnStop () if it goes into a hidden state.

Whether Onresume () or onStop ()
Onresume () Called before the activity starts interacting with the user. The activity is at the top of the activity stack, where the user can enter it.

The next callback method is always OnPause ().

Whether OnPause ()
OnPause () Called when the system is ready to start another activity that is recovering. This method is commonly used to commit unsaved changes to permanent data, stop animation playback, and other tasks that may consume CPU, and so on. It should complete the work very quickly, because the next activity will not be resumed until the method returns.

If the activity returns to the foreground, the next callback method is Onresume () and if the user is not visible, the next one is OnStop ()

OK Onresume () or onStop ()
OnStop () Called when activity is no longer visible to the user. The reason may be that it is about to be destroyed, or that other activity (existing or new) is resumed and will overwrite the activity.

If the activity also comes back to interact with the user, the next callback method is Onrestart (); If the activity is about to disappear, the next callback method is OnDestroy ()

OK Onrestart () or OnDestroy ()
OnDestroy () Called before the activity is destroyed. This is the last call the activity received. It may be because the activity has finished working (some people call finish here), or it may be because the system temporarily destroys the activity's instance in order to make room for it. You can use the Isfinishing () method to differentiate between the two cases. OK No

    • The full lifetime of the activity occurs between the OnCreate () call and the OnDestroy () call. Your activity should be in the OnCreate () method to complete all the "global" state settings (for example, define layout), and in the OnDestroy () method, release all the resources occupied. For example, if your activity has a thread running in the background that is used to download data from the network, you should create the thread in the OnCreate () method and stop the thread in the OnDestroy () method.
    • The visible lifetime of the activity occurs between the OnStart () call and the OnStop () call. During this time, users can see the activity on the screen and interact with it. For example, when a new activity is started and the OnStop () method is called, the activity cannot be seen. Between these two methods, you can manage the resources needed to display the activity. For example, you can register a broadcastreceiver in the OnStart () method to monitor changes that affect the user interface. And when the user no longer sees your display, write it off in the OnStop () method. The system calls OnStart () and OnStop () multiple times throughout the lifetime of the activity, as activity may be constantly switching back and forth between display and hide.
    • The activity's foreground lifetime occurs between the Onresume () call and the OnPause (). During this time, the activity is positioned before all other activity on the screen and has the user's input focus. Activity can enter and exit the foreground frequently-for example, OnPause () is called when the device enters hibernation or when a dialog box pops up. Because this state can occur frequently, the code in both methods should be fairly lightweight in order to avoid user waits caused by switching delays.

Life cycle Learning for Android

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.