Life cycle interaction of multiple activity for Android notes

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.