title: Unable to Pause Activity - Illegal State Exception while playing bgm in android
body: <p>(I've already solved it, just scroll down until EDIT 2)</p>
  
  <p>I have an application that, upon opening, plays a background music on loop. I decided I could take it a notch further and pause the bgm when the app is paused and played where it paused on resume. It works fine.</p>
  
  <p>However, the problem I encounter is when I exit the application. It gives the error: 
  <code>10-07 22:50:04.535: E/AndroidRuntime(5852): java.lang.RuntimeException: Unable to pause activity {com.cs119.megamanstrikes/com.cs119.megamanstrikes.MainActivity}: java.lang.IllegalStateException</code></p>
  
  <p>My onCreate looks like:</p>
  
  <pre><code>@Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      backgroundMusic = MediaPlayer.create(MainActivity.this, R.raw.mmbn_3);
      backgroundMusic.setLooping(true);
      backgroundMusic.start();
  
      Button start = (Button) findViewById(R.id.Start);
      Button highScore = (Button) findViewById(R.id.highScore);
      Button quit = (Button) findViewById(R.id.quit);
  
      start.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v){
              startGame();
          }
      });
  
      highScore.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v){
              highScore();
          }
      });
  
      quit.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v){
              quit();
          }
      });
  
  
  }
  </code></pre>
  
  <p>These are the overrides for onPause and onResume:</p>
  
  <pre><code>@Override 
  public void onPause(){
     super.onPause();
     backgroundMusic.pause();
  }
  
  @Override
  public void onResume(){
      super.onResume();
      backgroundMusic.start();
  }
  </code></pre>
  
  <p>And when the user clicks on the quit button or the back button:</p>
  
  <pre><code>@Override
  public void onBackPressed(){
      quit();
  }
  
  public void quit(){
      quitDialog();
  }
  
  private void quitDialog() {
      AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
  
      // Set title.
      alertDialogBuilder.setTitle("QUIT");
  
      // Set dialog message.
      alertDialogBuilder
              .setMessage("Are you sure you want to quit?")
              .setCancelable(false)
              .setPositiveButton("Yes",
                      new DialogInterface.OnClickListener() {
                          public void onClick(DialogInterface dialog, int id) {
                              //backgroundMusic.stop();
                              backgroundMusic.release();
                              finish();
                          }
                      })
              .setNegativeButton("No", new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int id) {
                      // if this button is clicked, resume teh gaem
                      //inputName();
                  }
              });
  
      // Create alert dialog.
      AlertDialog alertDialog = alertDialogBuilder.create();
  
      // Show it.
      alertDialog.show();
  }
  </code></pre>
  
  <p>After clicking "Yes" to quit the game, the app freezes and force closes. Here's the full LogCat error</p>
  
  <pre><code>10-07 22:50:04.535: E/AndroidRuntime(5852): FATAL EXCEPTION: main
  10-07 22:50:04.535: E/AndroidRuntime(5852): java.lang.RuntimeException: Unable to pause activity {com.cs119.megamanstrikescom.cs119.megamanstrikes.MainActivity}:java.lang.IllegalStateException
  10-07 22:50:04.535: E/AndroidRuntime(5852): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2358)
  10-07 22:50:04.535: E/AndroidRuntime(5852): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2315)
  10-07 22:50:04.535: E/AndroidRuntime(5852): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2295)
  10-07 22:50:04.535: E/AndroidRuntime(5852): at android.app.ActivityThread.access$1700(ActivityThread.java:117)
  10-07 22:50:04.535: E/AndroidRuntime(5852): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:946)
  10-07 22:50:04.535: E/AndroidRuntime(5852): at android.os.Handler.dispatchMessage(Handler.java:99)
  10-07 22:50:04.535: E/AndroidRuntime(5852): at android.os.Looper.loop(Looper.java:123)
  10-07 22:50:04.535: E/AndroidRuntime(5852): at android.app.ActivityThread.main(ActivityThread.java:3691)
  10-07 22:50:04.535: E/AndroidRuntime(5852): at java.lang.reflect.Method.invokeNative(Native Method)
  10-07 22:50:04.535: E/AndroidRuntime(5852): at java.lang.reflect.Method.invoke(Method.java:507)
  10-07 22:50:04.535: E/AndroidRuntime(5852): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
  10-07 22:50:04.535: E/AndroidRuntime(5852): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
  10-07 22:50:04.535: E/AndroidRuntime(5852): at dalvik.system.NativeStart.main(Native Method)
  10-07 22:50:04.535: E/AndroidRuntime(5852): Caused by: java.lang.IllegalStateException
  10-07 22:50:04.535: E/AndroidRuntime(5852): at android.media.MediaPlayer._pause(Native Method)
  10-07 22:50:04.535: E/AndroidRuntime(5852): at android.media.MediaPlayer.pause(MediaPlayer.java:1001)
  10-07 22:50:04.535: E/AndroidRuntime(5852): at com.cs119.megamanstrikes.MainActivity.onPause(MainActivity.java:163)
  10-07 22:50:04.535: E/AndroidRuntime(5852): at android.app.Activity.performPause(Activity.java:3877)
  10-07 22:50:04.535: E/AndroidRuntime(5852): at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1191)
  10-07 22:50:04.535: E/AndroidRuntime(5852): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2345)
  10-07 22:50:04.535: E/AndroidRuntime(5852): ... 12 more
  </code></pre>
  
  <p>I've also tried to do <code>backgroundMusic.stop();</code> instead of <code>backgroundMusic.pause()</code> in the onPause method, however, upon resuming the application, the sound doesn't play even if <code>backgroundMusic.play()</code> is present in the onResume() Method. </p>
  
  <p>I've also noticed that the error is still there regardless of what I place in the onPause() and onResume() and I try to Quit the application. </p>
  
  <p>Thanks for the help. </p>
  
  <p>EDIT 1:
  I place system.out lines in the onPause and onResume overrides so that I can see when it is executed. Upon starting the application, onResume executes and upon closing the application, onPause executes. I am very confused on why this is happening since onResume is different from onCreate.</p>
  
  <p>EDIT 2:
  Okay I've solved the problem. The problem mainly comes from the quit button, as it executes these lines of code: </p>
  
  <pre><code>backgroundMusic.stop();
  backgroundMusic.release();
  finish();
  </code></pre>
  
  <p>It somehow goes to the onPause method and tries to execute <code>backgroundMusic.pause()</code>. Now the app Force Closes because the backgroundMusic has already been released and it couldn't finish the command <code>backgroundMusic.pause()</code>. </p>
  
  <p>I got around this problem by creating another global variable:
      boolean flag = false;</p>
  
  <p>This flag checks if the quit button has been selected and if the user wants to quit the game. Once the user wants to quit the game, it will set the flag to true. </p>
  
  <pre><code>flag = true;
  backgroundMusic.stop();
  backgroundMusic.release();
  finish();
  </code></pre>
  
  <p>I set the flag to true so that when it goes to the onPause() method from finish() (I may be wrong with regards to the activity life cycle of Android but it seems that onPause() is executed within or after finish()), this will execute:</p>
  
  <pre><code>@Override 
  public void onPause(){
      super.onPause();
      if(!flag){
          if(backgroundMusic.isPlaying()){
              backgroundMusic.pause();
          }
      }
  
  }
  </code></pre>
  
  <p>Here, it will only execute super.onPause() if the user wants to Quit the game. Otherwise, if the game was really paused (via the home button or something), then the Flag would still be false and the background music will be paused. </p>
  
  <p>I also modified onResume() to be safe:</p>
  
  <pre><code>@Override
  public void onResume(){
      super.onResume();
      if(!backgroundMusic.isPlaying()){
          backgroundMusic.start();
      }
  }
  </code></pre>