Examples of Android activity life cycle

Source: Internet
Author: User
Tags getmessage

 Packagecom.example.yanlei.yl2;ImportAndroid.app.AlertDialog;ImportAndroid.content.DialogInterface;ImportAndroid.content.DialogInterface.OnKeyListener;ImportAndroid.os.Bundle;Importandroid.support.v7.app.AppCompatActivity;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.widget.TextView; Public classMainactivityextendsappcompatactivity { Public Static FinalString TAG = "Life cycle:"; String Str= ""; TextView Ptextview=NULL; @Override Public voidonCreate (Bundle savedinstancestate) {//callback When this activity is created        Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); LOG.E (TAG,"OnCreate"); Try{Str= Str + "Create: \ n";        Ptextview.settext (STR); } Catch(Exception e) {//log.e (TAG, "Error:" +e.getmessage (), e);}} @Overrideprotected voidOnDestroy () {//callback When this activity is destroyed        Super. OnDestroy (); LOG.E (TAG,"OnDestroy"); Try{Str= Str + "destroy: \ n";        Ptextview.settext (STR); } Catch(Exception e) {//log.e (TAG, "Error:" +e.getmessage (), e);}} @Overrideprotected voidOnPause () {//callback When this activity is paused        Super. OnPause (); LOG.E (TAG,"OnPause"); Try{Str= Str + "pause: \ n";        Ptextview.settext (STR); } Catch(Exception e) {//log.e (TAG, "Error:" +e.getmessage (), e);}} @Overrideprotected voidOnrestart () {//callback When you start this activity again        Super. Onrestart (); LOG.E (TAG,"Onrestart"); Try{Str= Str + "reboot: \ n";        Ptextview.settext (STR); } Catch(Exception e) {//log.e (TAG, "Error:" +e.getmessage (), e);}} @Overrideprotected voidOnresume () {//callback when the interface showing this activity is displayed        Super. Onresume (); LOG.E (TAG,"Onresume"); Try{Str= Str + "continue: \ n";        Ptextview.settext (STR); } Catch(Exception e) {//log.e (TAG, "Error:" +e.getmessage (), e);}} @Overrideprotected voidOnStart () {//callback when using this activity to accept user actions        Super. OnStart (); LOG.E (TAG,"OnStart"); Try{Str= Str + "Start: \ n";        Ptextview.settext (STR); } Catch(Exception e) {//log.e (TAG, "Error:" +e.getmessage (), e);}} @Overrideprotected voidOnStop () {//callback When this activity is stopped        Super. OnStop (); LOG.E (TAG,"OnStop"); Try{Str= Str + "STOP: \ n";        Ptextview.settext (STR); } Catch(Exception e) {//log.e (TAG, "Error:" +e.getmessage (), e);        }    }}

The logs are as follows:

OnCreate
OnStart
Onresume

OnPause
OnStop
OnDestroy

Examples of Android activity life cycle

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.