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