Controlling MediaPlayer behavior with boolean flags
<p>My app plays background music using a MediaPlayer object. I store it in a .java file along with other global variables named - what else? - GlobalVariables, so to access it and the other variables, I use this call:</p>

<pre><code>GlobalVariables.mPlayer
</code></pre>

<p>I want to have the MediaPlayer continue audio playback when switching between Activities in my app, but to pause when the app loses focus. To try to achieve this, I declared a boolean value called keepPlaying, also stored in GlobalVariables such that it's accessible to all Activities in the app. The idea is to have the Activity check the boolean flag every time onStop() is called, and if it finds that keepPlaying = false, pause playback, otherwise do nothing (keep it playing). I set the flag to true just before each startActivity() call, and set it to false in every onResume() call.</p>

<p>If I understand the Activity lifecycle correctly, when switching Activities, my app would first call OnStop() on the current one. So first I set keepPlaying to true, then onStop() is called. The method includes three lines:</p>

<pre><code>super.onStop();
if(GlobalVariables.keepPlaying == false)
    GlobalVariables.mPlayer.pause();
</code></pre>

<p>So it <em>should</em> keep playback going whenever I make a startActivity call, but pause playback when I minimize the app. Then, in the next Activity's onResume() method, I set the flag to false:</p>

<pre><code>if(GlobalVariables.keepPlaying == true){
    GlobalVariables.keepPlaying = false;
}else{
    GlobalVariables.mPlayer.start();
</code></pre>

<p>However, the app isn't behaving the way I want. It still pauses audio playback when switching Activities. I've tried creating getter and setter methods for the keepPlaying flag, but that hasn't fixed it, so I know it's not an access issue.</p>

<p>Can anyone think of any reason why this method isn't working or how I can fix it?</p>

<p>Thank you!</p>

<p>[EDIT] As requested, here is a more complete code sample. I don't have access to my own computer right now but this should be an exact re-creation. Assume all necessary imports.</p>

<pre><code>public class GlobalVariables{

    public boolean keepPlaying;
    public MediaPlayer mPlayer;

    // Getter and setter methods, found to be unnecessary
    public void setKeepPlaying(boolean input){keepPlaying = input;}
    public boolean getKeepPlaying(){return keepPlaying;}
}

// First activity. Music plays after starting Activity, as expected.
public class FirstActivity extends Activity{

    @Override
    public void onCreate(...){
        super.onCreate(...);

        GlobalVariables.keepPlaying = false;
        GlobalVariables.mPlayer.create(this, R.raw.music);
    }

    @Override
    public void onStop(){
        super.onStop();

        // Check to see if we want to stop audio playback
        if(GlobalVariables.keepPlaying == false){
            GlobalVariables.mPlayer.pause();
    }

    @Override
    public void onResume(){
        super.onResume();

        // If audio is already playing, set keepPlaying to false. Otherwise, start audio playback.
        if(GlobalVariables.keepPlaying == true) //Thus audio is playing
            GlobalVariables.keepPlaying = false;
        else
            GlobalVariables.mPlayer.start();
    }

    public void launchNextActivity(){
        GlobalVariables.keepPlaying = true;
        startActivity(new Intent(FirstActivity.this, SecondActivity.class));
    }
</code></pre>

<p>}</p>

<pre><code>Code for SecondActivity is the same as FirstActivity. When starting SecondActivity, the music abruptly stops. When hiding the app and resuming it, the music begins playing again.
</code></pre>
