Life cycle comparison of Android activity and service

Source: Internet
Author: User

1Activity life cycle

Seven methods

1. void OnCreate (Bundle savedinstancestate)

when Activity is executed when first loaded. When we start a new program, the onCreate event of its main form is executed. If the Activity is destroyed ( after OnDestroy) and then reloaded into the Task , its The OnCreate event will also be re-executed.

2. void OnStart () activity becomes called when the user is visible on the screen.

3. void Onresume () activity is invoked when the activity starts interacting with the user (whether it is a startup or a restart, the method is always called).

4. void OnPause () activity is called when the CPU and other resources are paused or retracted , the method is used to save the active state, but also to protect the site, press stack bar!

The form is executed when it is swapped to the background.

5. void OnStop () is called when activity is stopped and turned into an invisible phase and subsequent life-cycle events.

executes after the OnPause event. If the form is not re-viewed by the user for a period of time, the OnStop event of the form is executed, or the user presses the back key directly, and the form is removed from the current Task the OnStop event for the form is also executed .

6. void Onrestart () Called when activity is restarted. The activity is still in the stack, not the start of a new activity.

After the OnStop event executes, if the form and its process are not destroyed by the system, and the user re-views the form, the form's Onrestart event is executed,Onrestart event will skip the form's onCreate Event Direct Execution OnStart event.

7. The void OnDestroy () activity is called when it is completely removed from system memory, and the method is called possibly because someone calls OnFinish directly () method or the system decides to stop the activity to free up resources!

executed when the Activity is destroyed. After the form's onStop event, the Activity is destroyed if the form is not viewed again .

the situation one separate The normal life course of Activity is this:Oncreate->onstart->onpause->onstop->ondestroy. For example: Run an Activity, perform some simple actions (not involving page jumps, etc.), and then press the back key to end.

scenario Two, there are twoActivity(aand theb), initially showinga, and then byaStartb, and then in thebBack toa, this timeaWhat is the life process supposed to be (awasbcompletely covered)? athe process of experience isOncreate->onstart->onresume->onpause->onstop->onrestart->onstart->onresume. This process illustrates the diagram, ifActivitywhen completely obscured by other interfaces, it is not completely destroyed when it enters the background,but stay inOnStopState, when it entered againawhen theOnrestart->onstart->onresume, and restore again.

situation Three, the basic situation is the same as two, but at this time a was b partially obscured (e.g. to b Add a dialog box theme Android: Theme= "@android: Style/theme.dialog")a the process experienced is: Oncreate->onstart->onresume->onpause->onresume So when the Activity is partially obscured,

situation Four,Open the program, starta, clicka, startAlertdialog, press the back key fromAlertdialogreturn. athe process of experience is:Oncreate->onstart->onresumewhen starting and exitingDialogwhen theActivitythe state is always unchanged, visible,Dialogactually belongs toacitivityan internal interface that does not affectacitivty's life cycle. IntoOnPause, and did not enterOnStop, fromActivity2after the return, executed theOnresume

2.Service life cycle

start service with Context.startservice ()

its life cycle is Context.startservice ()->oncreate ()->onstart ()->service running--> ( if called Context.stopservice ())->ondestroy ()->service shut down

ifServiceIs not running yet, theAndroidCall FirstonCreate ()and then callOnStart ();
ifServiceIs already running, only call theOnStart (), so aServiceof theOnStartmethod may be repeatedly called multiple times.  calledStopServicewhen the directOnDestroy,
if it is the caller who exits directly without invoking theStopServicewords,Servicewill always run in the background.
theServicewhen the caller starts up again, it can beStopServiceCloseService.
so callStartServiceThe life cycle is:onCreate-OnStart (can be called multiple times)-OnDestroy

for bindservice () start Service experience:
Context.bindservice ()->oncreate ()->onbind ()->service running-->onunbind (), OnDestroy () Service stop

Onbindwill be returned to the client oneIbindinterface instance,Ibindmethods that allow the client to callback the service, such as gettingServicethe State or other action of the run.
this time the caller (Context, such asActivity) Will andServicebound together,Contextquit,
Srevicewill be calledOnunbind->ondestroyexit accordingly.  

so callBindserviceThe life cycle is:onCreate-Onbind (only once, cannot be bound multiple times) --Onunbind-Ondestory.
One but destroyActivityIt's over, if you pressHomePut it in the background, then he won't quit.

PS:
in the Service each time the turn off process, only onStart can be called multiple times ( through multiple startservice Call ), other onCreate,onbind,onunbind ,ondestory can only be called once in a life cycle.

Ps:service Class Two startup methods:

? Context.startservice ()
? Context.bindservice ()

1. Invoking the StartService () method anywhere in the same application can start the service, and the system will callback the service class's OnCreate () and the OnStart () method. This starts the Service running in the background until the Context.stopservice () or Selfstop () method is called.
also if a The Service has been started, and the other code tries to call the StartService () method, and the OnCreate () is not executed, but the OnStart () is executed again.

2. Another bindservice () method means that the service is tied to the client class that invokes the service, and if the call to this client class is destroyed, the service is destroyed. One advantage of this method is that after the Bindservice () method executes the service will callback the above mentioned Onbind (), you can return from here a class that implements the Ibind interface, the class will be able to communicate with the client, such as getting service The State or other action of the run. If the service is not running, using this method to start the service will onCreate the () method without calling OnStart ().

Summary:

1. The purpose of StartService () is to callback the OnStart () method, which is called when the service does not exist, if the service exists (for example, a bindservice was previously called), and the OnCreate () method Then the OnCreate method of the service has been called) then StartService () will skip the OnCreate () method.

2. Bindservice () is intended to call back the Onbind () method, which is used to establish a bridge between the service and the caller, and is not responsible for more work (for example, a service needs to connect to the server), Bindservice is typically used to bind to an existing service (that is, services initiated through StartService).

Life cycle comparison of Android activity and service

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.