For a long time did not write blog, lazy, feeling a bit.
The life cycle of an activity is a piece of the following drawings:
Let's take a brief look at the code below. Some of the content looks at the code gaze:
Package Com.mxy;import Android.app.activity;import Android.content.context;import android.content.intent;import Android.content.res.configuration;import Android.os.bundle;import Android.util.log;import Android.view.View; Import android.widget.button;/** * http://blog.csdn.net/liuhe688/article/details/67334071. Start activity: The system calls the OnCreate method first, then calls the OnStart method, and finally calls onresume,activity into the execution state. 2. Current activity is covered by other activity or locked screen: The system invokes the OnPause method to suspend the execution of the current activity. 3. The current activity is returned to the foreground or unlock screen by the overwritten state: The system calls the Onresume method and enters the execution state again. 4. Current activity go to the new activity interface or press the home key to return to the main screen. itself back in the background: The system calls the OnPause method first, then calls the OnStop method and goes into a stagnant state. 5. The user backs back to this activity: the system calls the Onrestart method first, then calls the OnStart method, and finally calls the Onresume method and enters the execution state again. 6. The current activity is in a covered state or the background is not visible, that is, steps 2nd and 4th, the system is out of memory. Kills the current activity. The user then returns to the current activity: Call the OnCreate method, the OnStart method, and the Onresume method again. into the execution 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. * @author Mxy * */public class Mainactivity extends Activity {private static final String TAG = "Mxy"; Private context context = this; private int param = 1; The activity was created when called @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (Savedinstan Cestate); LOG.I (TAG, "onCreate called."); Setcontentview (R.layout.activity_main); Button btn = (button) Findviewbyid (R.ID.BTN); Btn.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) { Intent Intent = new Intent (context, targetactivity.class); StartActivity (Intent); } }); //activity was called when it was created or returned to the foreground from the background @Override protected void OnStart () {Super.onstart (); LOG.I (TAG, "OnStart called."); //activity is called @Override protected void Onrest when it returns to the foreground from the background.Art () {Super.onrestart (); LOG.I (TAG, "Onrestart called."); //activity is called @Override protected void Onresume () {super.onresume () when it is created or has been overwritten and returned to the foreground from the background; LOG.I (TAG, "Onresume called."); The//activity form is called when it Gets or loses focus, after onresume or after OnPause/* @Override public void onwindowfocuschanged (Boolean h Asfocus) {super.onwindowfocuschanged (hasfocus); LOG.I (TAG, "onwindowfocuschanged called."); }*///activity is overwritten to the following or lock screen when called @Override protected void OnPause () {super.onpause (); LOG.I (TAG, "onPause called."); It is possible that after executing onpause or onstop, the system resource is strained to kill the activity, so it is necessary to save persistent data here}//To exit the current activity or jump to a new activity when called @Override protected void OnStop () {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 () {Super.ondestroy (); LOG.I (TAG, "ondestory 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 button 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) {outstate.putint ("param", param); LOG.I (TAG, "onsaveinstancestate called. Put param: "+ param); Super.onsaveinstancestate (outstate); /** * The activity was called when the system was 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, the user initiates the activity. * In both cases onrestoreinstancestate will be called after OnStart. */@Override protected void Onrestoreinstancestate (Bundle savedinstancestate) {param = Savedinstancest Ate.getint ("param"); LOG.I (TAG, "onrestoreinstancestate called. Get param: "+ param); Super.onrestoreinstancestate (savedinstancestate); }//When android:configchanges= "Orientation|ke is specifiedYboardhidden "After the direction changes when the onconfigurationchanged is called//here is just the designation orientation does not work//assumptions <activity> configured the Android:screenorie The Ntation property. Will invalidate the android:configchanges= "orientation". @Override public void onconfigurationchanged (Configuration newconfig) {super.onconfigurationchanged (newconf IG); LOG.I (TAG, "onconfigurationchanged called."); Switch (newconfig.orientation) {case Configuration.ORIENTATION_PORTRAIT:setContentView (r.layout.ac Tivity_main); Break Case Configuration.ORIENTATION_LANDSCAPE:setContentView (R.layout.activity_main); Break } } }
Sample project: http://download.csdn.net/detail/mengxiangyue/7254175
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
Android self-priming -15-activity life cycle