1 Public classMainactivityextendsActivity {2 3 @Override4 protected voidonCreate (Bundle savedinstancestate) {5 Super. OnCreate (savedinstancestate);6 Setcontentview (r.layout.activity_main);7System.out.println ("OnCreate ()");8 }9 Ten @Override One protected voidOnStart () { A Super. OnStart (); -System.out.println ("OnStart ()"); - } the - @Override - protected voidOnresume () { - Super. Onresume (); +System.out.println ("Onresume ()"); - } + A @Override at protected voidOnPause () { - Super. OnPause (); -System.out.println ("OnPause ()"); - } - - @Override in protected voidOnStop () { - Super. OnStop (); toSystem.out.println ("OnStop ()"); + } - the @Override * protected voidOnDestroy () { $ Super. OnDestroy ();Panax NotoginsengSystem.out.println ("OnDestroy ()"); - } the + @Override A protected voidOnrestart () { the Super. Onrestart (); +System.out.println ("Onrestart ()"); -}
Rewrite the above methods in Android Studio's Mainactivity.java to learn about the life cycle of activity in Android.
Running the project first displays the following results:
Then look at the results shown by the Logcat tool: OnCreate (), OnStart (), Onresume () life cycle has reached activity is running
Then press the home button to run as follows:
Activity executes to OnPause (), OnStop () is not cleared for this process, and then clicking on the once running program shows the following:
The activity goes through the right side of Onrestart (), OnStart (), Onresume () again into the activity is running, when clicking the Back button shows the following:
Activity is killed by OnPause (), OnStop (), OnDestroy (), and the life cycle of the activity ends.
Life cycle of activity in Android