The life cycle of Android learning activity
Android apps cannot control their own processes themselves, but the Android runtime can manage each process. Therefore, the correct understanding of the life of activity, to ensure that users have a good experience has a great help.
1. Activity stack and statusThe activity stack is a collection of all activity LIFO that is currently running. The newly started activity is always at the top of the stack. Activity has four states.
- Active State When the activity is at the top of the stack, that is, activity is visible, with focus on the activity, you can accept user input at this time. Under normal circumstances, the Android runtime will try to keep the activity from being terminated.
- paused state the activity at this time is visible, but it has no focus. When a transparent or non-full-screen activity is in the current activity, activity is paused. Activity in this state is approximately active state. If memory is not required, normally it will not be terminated by runtime.
- Stop state When an activity is in a stopped state, it is not visible. But it is still in memory, saving the state of the current activity. When memory is needed elsewhere, the activity of this state is the preferred terminating object.
Inactive State After the activity is terminated, it is inactive. Activity in this state has been removed from the activity stack. To show activity in this state, you need to restart the activity.
life cycle of 2.Activitythe life cycle diagram of the activity is shown below.
3.MainActivity
1 PackageCom.example.zhouy.android4exercise;2 3 Importandroid.app.Activity;4 ImportAndroid.content.DialogInterface;5 ImportAndroid.content.Intent;6 ImportAndroid.os.Bundle;7 ImportAndroid.os.PersistableBundle;8 ImportAndroid.support.v7.app.AlertDialog;9 ImportAndroid.util.Log;Ten ImportAndroid.view.View; One ImportAndroid.widget.Button; A - Public classMainactivityextendsActivityImplementsview.onclicklistener{ - the PrivateButton malertbtn =NULL; - - PrivateButton mjumpbtn =NULL; - +@Override - protected voidOnCreate (Bundle savedinstancestate) { + Super. OnCreate (Savedinstancestate); ASetcontentview (R.layout.activity_main); atMALERTBTN = (Button) Findviewbyid (R.id.alert_dialog); -Malertbtn.setonclicklistener ( This); -MJUMPBTN = (Button) Findviewbyid (r.id.jump_to_another_activity); -Mjumpbtn.setonclicklistener ( This); -LOG.V ("Tag","onCreate ()"); -} in - Private voidAlertdialog () { to the activity's life cycle does not change when the activity is used to pop up a dialog box. + FinalAlertdialog.builder Builder =NewAlertdialog.builder ( This); -Builder.settitle ("dialog box title"); theBuilder.setmessage ("dialog box Contents"); *Builder.setpositivebutton ("OK",NewDialoginterface.onclicklistener () { $@OverridePanax Notoginseng Public voidOnClick (Dialoginterface Dialoginterface,inti) { -LOG.V ("Tag","Ok"); the} +}); ABuilder.setnegativebutton ("cancle",NewDialoginterface.onclicklistener () { the@Override + Public voidOnClick (Dialoginterface Dialoginterface,inti) { -LOG.V ("Tag","Cancel"); $} $}); -Builder.create (); -Builder.show (); the -}Wuyi the Private voidGotonextactivity () { -Intent Intent =NewIntent ( This, Nextactivity.class); WuStartActivity (Intent); -} About@Override $ protected voidOnrestart () { - Super. Onrestart (); -LOG.V ("Tag","Onrestart ()"); -} A +@Override the protected voidOnrestoreinstancestate (Bundle savedinstancestate) { - Super. Onrestoreinstancestate (Savedinstancestate); $LOG.V ("Tag","onrestoreinstancestate"); the} the the@Override the protected voidOnStart () { - Super. OnStart (); inLOG.V ("Tag","OnStart ()"); the the} About the@Override the protected voidOnresume () { the Super. Onresume (); +LOG.V ("Tag","Onresume ()"); - the}Bayi the the@Override - protected voidOnPause () { - Super. OnPause (); theLOG.V ("Tag","OnPause ()"); the the} the -@Override the Public voidOnsaveinstancestate (Bundle outstate, Persistablebundle outpersistentstate) { the Super. Onsaveinstancestate (Outstate, outpersistentstate); theLOG.V ("Tag","onsaveinstancestate");94} the the@Override the protected voidOnStop () {98 Super. OnStop (); AboutLOG.V ("Tag","onStop ()"); - 101}102 103@Override104 protected voidOnDestroy () { the Super. OnDestroy ();106LOG.V ("Tag","ondestory ()");107 108}109 the@Override111 Public voidOnClick (view view) { the intid = View.getid ();113 if(id = = R.id.alert_dialog) { theAlertdialog (); the}Else if(id = = r.id.jump_to_another_activity) { theGotonextactivity ();117}118}119} -
4.activity_main.xml
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Relativelayout xmlns:Android="Http://schemas.android.com/apk/res/android"3 xmlns:Tools="Http://schemas.android.com/tools"4 Android:Layout_width="Match_parent"5 Android:Layout_height="Match_parent"6 Android:Paddingbottom="@dimen/activity_vertical_margin"7 Android:paddingleft="@dimen/activity_horizontal_margin"8 Android:paddingright="@dimen/activity_horizontal_margin"9 Android:paddingtop="@dimen/activity_vertical_margin"
Ten Tools:Context="Com.example.zhouy.android4exercise.MainActivity"> One <Button A Android:ID="@+id/alert_dialog" - Android:Layout_alignparenttop="true" - Android:Layout_width="Match_parent" the Android:Layout_height="Wrap_content" - Android:text="Pop-up dialog box"/> - <Button - Android:ID="@+id/jump_to_another_activity" + Android:Layout_below="@id/alert_dialog" - Android:Layout_width="Match_parent" + Android:Layout_height="Wrap_content" A Android:text="Jump to another activity"/> at - </Relativelayout>
Life cycle of activity