Android---7---life cycle of activities and activities

Source: Internet
Author: User

What is activity

Simply put, activity is the interactive interface that fills the window or hovers over other windows, which is what is displayed on the phone screen.

In an application that typically consists of multiple activity, a master activity is specified in the manifest.xml. The two lines of code in Intent-filter are important to indicate that the activity is the primary activity.

This activity is visible when the program is first run, and the activity can be manipulated by initiating other activity. When other activities are started, the current activity is stopped, pushed into the bottom of the stack, and the new activity is run at the top of the stack. When the user presses the back key, the current activity is destroyed and the previous activity resumes running.

Life cycle of an activity:

Seven methods of activity with three stages:

1.onCreate ()

The method that each activity will have, called when the activity is first created

2.OnStart

This activity 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, at which point the activity must be at the top of the stack at the back 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.

Three stages:

1. Start activity: The methods for performing 3 life cycles at this stage, respectively: OnCreate, OnStart, and Onresume methods

2.Activity regain focus: If activity regain focus, 3 methods are executed sequentially, Onrestart, OnStart, and Onresume

3. Close activity: When activity is closed, the system executes 3 methods, OnPause, OnStop, and ondestory in turn.




From the life cycle of the activity above, it is not difficult to see that the graph contains a two-layer loop, the first loop is: Onpause->onresume->onpause

The second layer of the loop is: onstart->onrestart->onresume->onpause->onstop

The first loop is called the focus life cycle, and the second layer of the loop can be considered a life cycle. That is, the first loop loops through the process of acquiring and losing the activity focus, during which the activity is always visible

The second loop is a loop in the process of visible and invisible activity.



Package Com.example.android_life;import Android.app.activity;import Android.os.bundle;import android.util.Log; Import Android.view.menu;import Android.view.menuitem;public class Mainactivity extends Activity {private String TAG = "M Ainactivity ";p rotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); LOG.I (TAG, "onCreate is executed");} @Overrideprotected void OnStart () {//TODO auto-generated method Stubsuper.onstart (); LOG.I (TAG, "OnStart is executed");} @Overrideprotected void Onrestart () {//TODO auto-generated method Stubsuper.onrestart (); LOG.I (TAG, "Onrestart is executed");} @Overrideprotected void Onresume () {//TODO auto-generated method Stubsuper.onresume (); LOG.I (TAG, "Onresume is executed");} @Overrideprotected void OnStop () {//TODO auto-generated method Stubsuper.onstop (); LOG.I (TAG, "OnStop is executed");} @Overrideprotected void OnPause () {//TODO auto-generated method Stubsuper.onpause (); LOG.I (TAG, "OnPause isExecuted ");} @Overrideprotected void OnDestroy () {//TODO auto-generated method Stubsuper.ondestroy (); LOG.I (TAG, "OnDestroy is executed");}}

When the program is deployed to the emulator, the OnCreate OnStart, Onresume (Create-start-to-run) method appears, when you press the dial-up button, a onpause, onStop (Pause-to-stop) method appears. Press the back key Onrestart onStart Onresume (restart-and-start-run) exit key OnPause OnStop ondestory (Suspend--stop---destroy)


Android---7---life cycle of activities and activities

Related Article

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.