Single activity:
Invoke when application is open: OnCreate (), OnStart (), Onresume ();
Press the back key: OnPause (), OnStop (), ondestory ();
When interacting between multiple activity:
To jump to a second activity:
| Firstactivity |
Secondactivity |
| OnPause () |
|
|
OnCreate () |
|
OnStart () |
|
Onresume () |
| OnStop () |
|
Press the lock screen key
| Secondactivity |
| OnPause () |
| OnStop () |
Unlock
| Secondactivity |
| Onrestart () |
| OnStart () |
| Onresume () |
Press the back key, or Secondactivity call finish ()
| Firstactivity |
Secondactivity |
|
OnPause () |
| Onrestart () |
|
| OnStart () |
|
| Onresume () |
|
|
OnStop () |
|
Ondestory () |
Life Cycle Application Examples
Play Music in activity
OnCreate ():
1 PrivateMediaPlayer MediaPlayer;2 @Override3 protected voidonCreate (Bundle savedinstancestate) {4 Super. OnCreate (savedinstancestate);5 Setcontentview (r.layout.activity_main);6LOG.I ("Imformation", "Mainactivity oncreate!");7Mediaplayer=mediaplayer.create ( This, r.raw.quite);8 Mediaplayer.start ();9 Ten}
Music also needs to be paused while activity OnPause
At this point, you need a variable where the record is played: private int place;
OnPause:
1 @Override2 protected voidOnPause () {3 //TODO auto-generated Method Stub4 Super. OnPause ();5 if(Mediaplayer.isplaying ())6 {7 mediaplayer.pause ();8Place=mediaplayer.getcurrentposition ();9 Ten } OneLOG.I ("Imformation", "Mainactivity onpause!"); A}
Similarly, when the activity is resume, the music will be played
Onresume:
1 @Override2 protected voidOnresume () {3 //TODO auto-generated Method Stub4 Super. Onresume ();5 if(place!=0)6 {7 Mediaplayer.seekto (place);8 Mediaplayer.start ();9 }TenLOG.I ("Imformation", "Mainactivity onresume!"); One}
The music player is destroyed when the activity is destroyed.
1 @Override2 protected voidOnDestroy () {3 //TODO auto-generated Method Stub4 Super. OnDestroy ();5 if(mediaplayer!=NULL){6 mediaplayer.release ();7Mediaplayer=NULL;8 }9LOG.I ("Imformation", "Mainactivity ondestory!");Ten}
Note: This article is to learn MU class network android-activity (http://www.imooc.com/learn/384) when the note, if there is infringement, delete immediately
Life cycle interaction of multiple activity for Android notes