I. Overview:
Activity is one of the four components of Android, is a user interface program, it will provide users with an interactive interface function. It is the basic functional unit of Android application, in fact, the activity mechanism in Android is similar to the servlet, the Android system is equivalent to a servlet container, the activity is equivalent to a servlet, Our activity is in this container, all the processes of creating instances, initializing, destroying instances are called by the container, and the activity itself has no interface. So the activity class creates a window where the developer can put the UI on the activity-created window via the Setcontentview (View) interface.
second, life cycle
1,onCreate: Create the interface here, do some initialization of data operations
2,OnStart: The user visible but not interactive state
3,Onresume: Can interact with the user state (the activity stack system to manage activity through the stack)
4,onPause: The user can not see the interactive state, the system will stop the animation and other CPU-consuming things, should be here to save some of your data, because this time your program's priority is reduced, it may be recalled by the system. The data that is stored here should be read in the Onresume.
5.onStop: Not visible, covered by the next activity
6,OnDestroy: This is the last time the activity was killed before the method is called, may be other classes call the Finish method or the system in order to save space to temporarily kill it, you can use isfinishing () to judge it, If you have a progress dialog running in the thread, please cancel it in OnDestroy, or the Cancel method called dialog will throw an exception when the threads end. Onpause,onstop, OnDestroy, in three different states, the activity can be killed by the system.
Packagecom.example.activity;Importandroid.app.Activity;ImportAndroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button; Public class mainactivity extends Activity { Private Static FinalString TAG ="Lifecycleactivity";Private intparam =1;PrivateButton button;//activity is called when it is created @Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); LOG.I (TAG,"onCreate called."); Setcontentview (R.layout.activity_main); Button = (Button) This. Findviewbyid (R.id.button1); Button.setonclicklistener (NewOnclicklistener () {@Override Public void OnClick(View arg0) {//TODO auto-generated method stubIntent Intent =NewIntent (mainactivity. This, Targetactivity.class); StartActivity (Intent); } }); }called when//activity is created or the background is back to the foreground @Override protected void OnStart() {//TODO auto-generated method stub Super. OnStart (); LOG.I (TAG,"OnStart called."); }//activity is called when it returns to the foreground from the background @Override protected void Onrestart() {//TODO auto-generated method stub Super. Onrestart (); LOG.I (TAG,"Onrestart called."); }//activity is called when it is created or is overwritten and back to the foreground from the background @Override protected void Onresume() {//TODO auto-generated method stub Super. Onresume (); LOG.I (TAG,"Onresume called."); }//activity is called when it is overwritten to the bottom or lock screen @Override protected void OnPause() {//TODO auto-generated method stub Super. OnPause (); LOG.I (TAG,"OnPause called.");//It is possible that when the OnPause or onstop is executed, the system resources are strained to kill the activity, so it is necessary to save persistent data here}is called when exiting the current activity or jumping to a new activity @Override protected void OnStop() {//TODO auto-generated method stub Super. OnStop (); LOG.I (TAG,"OnStop called."); }//is called when exiting the current activity and the activity ends after the call @Override protected void OnDestroy() {//TODO auto-generated method stub Super. OnDestroy (); LOG.I (TAG,"OnDestroy called."); }//activity is called when the window Gets or loses focus, after onresume or after OnPause @Override Public void onwindowfocuschanged(BooleanHasfocus) {//TODO auto-generated method stub Super. onwindowfocuschanged (Hasfocus); LOG.I (TAG,"onwindowfocuschanged called."); }/** * is called when the activity is killed by the system. * For example: when the screen orientation changes, the activity is destroyed and rebuilt; The current activity is in the background and the system resources are strained to kill it. * In addition, this method is also called when jumping to other activity or pressing the home key to return to the main screen, in order to save the state of the current view component. * Called before OnPause. */ @Override protected void onsaveinstancestate(Bundle outstate) {//TODO auto-generated method stubLOG.I (TAG,"onsaveinstancestate called.");Super. Onsaveinstancestate (Outstate); }/** * Activity is called when the system is killed and then rebuilt. * For example: when the screen orientation changes, the activity is destroyed and rebuilt, the current activity is in the background, the system resources are strained to kill, and the user initiates the activity. * In both cases onrestoreinstancestate will be called after OnStart. */ @Override protected void onrestoreinstancestate(Bundle savedinstancestate) {//TODO auto-generated method stubLOG.I (TAG,"onrestoreinstancestate called.");Super. Onrestoreinstancestate (Savedinstancestate); }}
Process:
1, start the activity, first call the OnCreate method, then call the OnStart method, and finally call Onresume,activity into the running state.
2, when the activity is covered by other activity or lock screen, the system will call the OnPause method, suspend the execution of the current activity.
3, the current activity from the covered state back to the foreground or unlock screen: The system will call the Onresume method, again into the running state.
4, the current activity to go to the new activity interface or press the home button back to the main screen, self-retired: The system will first call the OnPause method, and then call the OnStop method, into a stagnant state.
5, the user back to this activity: the system will first call the Onrestart method, then call the OnStart method, and finally call the Onresume method, again into the running state.
6, the current activity is in the covered state or the background is not visible, that is, the 2nd and 4th steps, the system memory is insufficient, kill the current activity, and then the user returned to the current activity: Call OnCreate method Again, OnStart method, Onresume method To enter a running state.
7, the user exits the current activity: The system calls the OnPause method first, then calls the OnStop method, and finally calls the Ondestory method to end the current activity.
8. Onwindowfocuschanged method: Called when the activity window Gets or loses focus, such as when it was first presented to the user at the time of creation, and the current activity is overwritten by other activity The current activity goes to another activity or presses the home key back to the main screen, and the user exits the current activity. In these cases, onwindowfocuschanged is called, and when the activity is created it is called after Onresume, when the activity is overwritten or retired or the current activity exits, It is called after OnPause:
This method is useful in some situations, for example, when the program starts to get the size of a particular view component, it may not be available in OnCreate because the Window object is not created yet, and we need to get it in onwindowfocuschanged.
9, Onsaveinstancestate: (1) After the activity is covered or back to the background, the system resources are not enough to kill, this method will be called, (2) when the user changes the orientation of the screen, this method will be called; (3) This method is called when the current activity jumps to another activity or presses the home key to return to the main screen. In the first case, we cannot guarantee when the system will be dispatched according to the degree of resource stress; the second is the screen flipping direction, the system first destroys the current activity, and then rebuilds a new one, we can save some temporary data when we call this method. In the third case, the system calls this method to save the state of each view component in the current window. The order in which the onsaveinstancestate is called precedes OnPause.
10, Onrestoreinstancestate: (1) After the activity is covered or back to the background, the system resources are not enough to kill it, and then the user returned to the activity, this method will be called, (2) in the process of the user changing the screen orientation, This method is called. We can override this method so that some temporary data can be recovered. The order of Onrestoreinstancestate is after OnStart
Three, the screen switch
Packagecom.example.activity;Importandroid.app.Activity;ImportAndroid.content.res.Configuration;ImportAndroid.os.Bundle;ImportAndroid.util.Log; Public class mainactivity extends Activity { Private Static FinalString TAG ="Mainactivity";Private intparam =1;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (r.layout.orientation_portrait); LOG.I (TAG,"onCreate called."); }@Override protected void OnStart() {Super. OnStart (); LOG.I (TAG,"OnStart called."); }@Override protected void Onrestart() {Super. Onrestart (); LOG.I (TAG,"Onrestart called."); }@Override protected void Onresume() {Super. Onresume (); LOG.I (TAG,"Onresume called."); }@Override protected void OnPause() {Super. OnPause (); LOG.I (TAG,"OnPause called."); }@Override protected void OnStop() {Super. OnStop (); LOG.I (TAG,"OnStop called."); }@Override protected void OnDestroy() {Super. OnDestroy (); LOG.I (TAG,"ondestory called."); }@Override protected void onsaveinstancestate(Bundle outstate) {Outstate.putint ("param", param); LOG.I (TAG,"Onsaveinstancestate called. Put param: "+ param);Super. Onsaveinstancestate (Outstate); }@Override protected void onrestoreinstancestate(Bundle savedinstancestate) {param = Savedinstancestate.getint ("param"); LOG.I (TAG,"Onrestoreinstancestate called. Get param: "+ param);Super. Onrestoreinstancestate (Savedinstancestate); }//When android:configchanges= "orientation" is specified, onconfigurationchanged is called when the direction changes @Override Public void onconfigurationchanged(Configuration newconfig) {Super. onconfigurationchanged (Newconfig); LOG.I (TAG,"onconfigurationchanged called.");Switch(newconfig.orientation) { CaseConfiguration.ORIENTATION_PORTRAIT:setContentView (r.layout.orientation_portrait); Break; CaseConfiguration.ORIENTATION_LANDSCAPE:setContentView (R.layout.orientation_landscape); Break; } }}
I. No Settings Configchanges property
1. Rotate the screen
When the screen is rotated, the system destroys the current activity and rebuilds a new one, and the system first calls the Onsaveinstancestate method, and we save a temporary parameter inside the bundle object. Then we successfully removed the parameter after the activity was rebuilt.
If we want to avoid re-creation, we need to android:configchanges= "orientation" to the orientationactivity corresponding configuration in Androidmainfest.xml;
* Two. Add Configchanges Orientation property in Androidmanifest
<?xml version= "1.0" encoding= "Utf-8"?><manifest xmlns:android="Http://schemas.android.com/apk/res/android" package ="Com.screen" android:versioncode= "1" android:versionname=" 1.0 "> <application android:icon="@drawable/icon" android:label= "@string/app_name"> <activity android:name=". Mainactivity " android:label=" @string/app_name "android:configchanges=" Orientation "> <intent-filter> <action android:name="Android.intent.action.MAIN" /> <category android:name="Android.intent.category.LAUNCHER" /> </intent-filter> </activity> </Application></manifest>
With four rotations, you can see that only the Onconfigurationchanged method is called in each direction of rotation without destroying the rebuild process.
Here are a few things to keep in mind:
1. If the Android:screenorientation attribute is configured, android:configchanges= "orientation" is invalidated.
2. The simulator differs greatly from the real machine: If the simulator does not match the Android:configchanges property or the configuration value is orientation, cut to the horizontal screen to perform a destruction--rebuild, cut to the vertical screen to perform two times. The real machine is one time. If the emulator is configured with android:configchanges= "Orientation|keyboardhidden" (if Android4.0, it is "orientation|keyboardhidden| ScreenSize "), cut the vertical screen to perform a onconfigurationchanged, cut the horizontal screen to perform two times. The real machine is one time.
Activity life cycle and the robustness of the program is closely related to the hope that friends can seriously understand, skilled application.
Android activity: life cycle of activity