introduction of activity lifecycle
We learn the lifecycle of the servlet when we are learning the Java Web, so we have a certain understanding of the concept of life cycle, simply that the process of something from birth to death.
The activity also has a declaration cycle, which is created to the last to be destroyed, and events may be affected by a number of unexpected incidents, such as when a call is made when a function is used, the activity needs to be set in the lifecycle functions;
The following figure is the most classic diagram in the activity lifecycle:
7 Life cycle methods:
(1) onCreate (Bundle Bundle); Create activity
(2) OnStart (), calling when an activity is opened
(3) Onresume (); Called when the activity is displayed
(4) OnPause (); Called when an activity is overwritten but not completely overwritten
(5) OnStop (); Called when an activity is overwritten
(6) Onrestart (), called when an activity is overwritten and then displayed again
(7) OnDestroy (), last destroyed, call
These methods can be summed up with several rules:
(1) onCreate ()-->onstart ()-->onresume () to begin the process of displaying activity
(2) Onresume ()-->onpause () The activity is not at the front, but the visible process
(3) Onresume ()-->onpause ()-->onstop () is a process covered by the activity
(4) OnPause ()-->onresume () is the process from which the activity is visible but not to the front and back to the front.
(5) OnStop ()-->onrestart ()-->onstart ()-->onresume () for activity from not visible in the front of the process
second, non-lifecycle but important method
When an application encounters an unexpected condition, such as low memory, the user presses the home key and so on to need to save some temporary data, for example you fills in the EditText to write half, you pressed the wrong key, presses into the home key, if the default situation, will lose all input information, but this will have the loss to the user, So we need the following two methods:
1.onSaveInstanceState (Bundle Bundle); Save data in case of accident
Save the data call when the accident occurs
From run state to pause state or stop state call
Called when the screen rotates
2.onRestoreInstanceState (Bundle Bundle); Recover data
Like what:
We want to keep the information that EditText entered, so the code is:
Package com.xiazdong.activity.life;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.widget.EditText;
public class Mainactivity extends activity {
private edittext edittext;
@Override public
void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.main);
EditText = (edittext) This.findviewbyid (R.id.edittext);
}
@Override
protected void onrestoreinstancestate (Bundle savedinstancestate) {
String name = Savedinstancestate.getstring ("name"); Restore the EditText data
edittext.settext (name);
Super.onrestoreinstancestate (savedinstancestate);
}
@Override
protected void onsaveinstancestate (Bundle outstate) { //Save EditText Data
outstate.putstring ( "Name", Edittext.gettext () + "");
Super.onsaveinstancestate (outstate);
}
Third, some other settings
<activity android:theme= "@android: Style/theme.dialog"/> can set activity into a window mode;