Android life cycle diagram
Android life cycle, top down, below we pass a simple demo, each life cycle.
Through demo instructions
1, here my mainactivity rewrite the 7 methods of the parent class (the method of gray shading in the figure)
Code
PackageCom.example.helloworld;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.view.Menu; Public class mainactivity extends Activity { @Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); System.out.println ("OnCreate"); Setcontentview (R.layout.activity_main); }@Override protected void OnStart() {Super. OnStart (); System.out.println ("Onstrat"); }@Override protected void Onresume() {Super. Onresume (); System.out.println ("Onresume"); }protected void OnPause() {Super. OnPause (); System.out.println ("OnPause"); }protected void OnStop() {Super. OnStop (); System.out.println ("OnStop"); }protected void OnDestroy() {Super. OnPause (); System.out.println ("OnDestroy"); }protected void Onrestart() {Super. Onrestart (); System.out.println ("Onrestart"); }@Override Public Boolean Oncreateoptionsmenu(Menu menu) {//inflate the menu; This adds items to the action bar if it is present.Getmenuinflater (). Inflate (R.menu.main, menu);return true; }}
2. Launch apk, view log, output sequence Oncreate->onstart->onresume
3, in our mobile phone keys have 2 exit button, one is home, one is the return button.
4, we click the Home button (small house), output order Onpause->onstop
5, then we go into the application, we can look at the life cycle diagram, compared to, that is, click on the home exit, the application is thrown in the background, not destroyed, start again, do not need to call the OnCreate method
6, below we click, return button exit, executed the Ondestory method
7, restart the application, call the OnCreate method to re-create
Through the above steps of debugging, you can analyze an application from start to exit, to restart the background of a running situation.
Android-Life cycle