Before writing this blog post, have a certain understanding and understanding of Android. This blog post focuses on the life cycle of Android activity, which is what Android developers need to know. Android's activity component has 7 lifecycles: OnCreate (), OnStart (), Onresume (), OnPause (), OnStop (), Ondestory (), Onrestart ().
These 7 lifecycles are called at 4 specific stages, respectively.
Start activity: Perform 3 lifecycles in turn when activity starts: OnCreate, OnStart, Onresume.
Activity loses focus: If you enter another focus from one activity, the current activity executes: OnPause, OnStop.
Activity regain focus: When returning this activity: Onrestart, OnStart, Onresume.
Closed Activity:activity is closed: OnPause, OnStop, Ondestory.
Activity life cycle Flowchart:
See two loops of activity life cycle: 1.onResume, OnPause, Onresume;2.onstop, Onrestart, OnStart, Onresume, OnPause, OnStop
There are three life cycles of activity:
Overall life cycle: OnCreate, OnStart, Onresume, OnPause, OnStop, ondestory
Visual life cycle: OnStart, Onresume, OnPause, OnStop
Focus life cycle: Onresume, OnPause
Code to verify Android life cycle
protected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); LOG.D ("Test", "OnCreate"); } @Overrideprotected voidOnStart () {//TODO auto-generated Method Stub Super. OnStart (); LOG.D ("Test", "OnStart"); } @Overrideprotected voidOnresume () {//TODO auto-generated Method Stub Super. Onresume (); LOG.D ("Test", "Onresume"); } @Overrideprotected voidOnPause () {//TODO auto-generated Method Stub Super. OnPause (); LOG.D ("Test", "OnPause"); } @Overrideprotected voidOnStop () {//TODO auto-generated Method Stub Super. OnStop (); LOG.D ("Test", "OnStop"); } @Overrideprotected voidOnDestroy () {//TODO auto-generated Method Stub Super. OnDestroy (); LOG.D ("Test", "Ondestory"); } @Overrideprotected voidOnrestart () {//TODO auto-generated Method Stub Super. Onrestart (); LOG.D ("Test", "Onrestart"); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {//Handle Action Bar item clicks here. The Action Bar would//automatically handle clicks on the Home/up button, so long//As you specify a the parent activity in Androidmanifest.xml. intID =Item.getitemid (); if(id = =r.id.action_settings) { return true; } return Super. onoptionsitemselected (item); }
Life cycle Testing
The program just started running
:
Program loses focus
The activity life cycle of Android four components