antonixic
antonixic

Reputation: 3

How do I know when application is in the background?

I have an application that is a menu with several options, every option when chosen will lead to a new activity. I also have music playing in the background using a service. I want my music play continuously no matter what activity is focused.

My problem is:

So what should I do now?

Upvotes: 0

Views: 61

Answers (1)

Sam
Sam

Reputation: 86948

You can override onPause() and use a boolean flag.
Set the boolean to true if you are launching a new activity. When onPause() is called you'll know if it is not true then you should stop the music.

boolean keepPlaying = false;

protected void onPause() { 
    if(!keepPlaying)
        stopService(new Intent(this, MediaPlayerServices.class)); 
    super.onPause();
}

Upvotes: 1

Related Questions