title: Android Stop Background Music
body: <p>EDIT - FOUND A EASY 5-10 LINE SOLUTION!!!  See MY OWN ANSWER BELOW!!!  YAY!!!!!!!!!</p>
  
  <p>I've searched for 5 hours, dozens of SO posts, no answers, and this seems like the most simple obvious freaking thing!!!</p>
  
  <p>EDIT- btw, this is NOT a music player app, just an app to view pics and text, opens multiple activities like menu and about, view diff types of pictures etc.  I just want to play some simple background music while looking thru the pics and text, why is that so difficult?</p>
  
  <p>another EDIT - it seems the main question really is:
  "WHY DOES PRESSING THE HOME BUTTON NOT CALL onPause or onStop ???" -so I can know when to stop the media player?  and how do the games I download on the market accomplish this???</p>
  
  <p>home activity starts</p>
  
  <p>then</p>
  
  <p>media player starts:</p>
  
  <pre><code>player = MediaPlayer.create(this, R.raw.idil);
  player.setLooping(true);
  player.setVolume(100,100);
  player.start();
  </code></pre>
  
  <p>Player declared outside of onCreate</p>
  
  <pre><code>MediaPlayer player;
  </code></pre>
  
  <p>When other activities are opened, the background music continues uninterrupted, which is GOOD, that is what I want.</p>
  
  <p>Now, when I am done with my <strong>very</strong> simple app (that just shows some pics and texts in diff activities), I either click BACK multiple times to get to the home/original activity, and then one more time to "exit", OR, I simply press home to "exit" because I'm DONE with this app and I do not need to hear that music anymore.</p>
  
  <hr>
  
  <p>OPTION 1</p>
  
  <p>Call <code>player.stop();</code> in an <code>onPause</code> override, this is <strong>not</strong> what I want because background music stops when I leave the home activity for other activities like 'menu' and 'about', I also do not what to use pause and resume when opening new activities because I do not want the pretty background music to 'skip' or be interrupted.</p>
  
  <hr>
  
  <p>OPTION 2</p>
  
  <pre><code>@Override
      protected void onPause() {
          super.onPause();
          if (this.isFinishing()){
              player.stop();
          }
      }
  </code></pre>
  
  <p>This is better because background music does not stop between activities, and when I press BACK from my home activity, the music stops, and I can continue to enjoy my android fun phone in peace and quiet, <strong>but</strong> the problem is, when pressing the HOME button to "exit" my app, that pesky background music keeps playing.</p>
  
  <hr>
  
  <p>ODDLY</p>
  
  <pre><code>@Override
  protected void onStop() {
          super.onStop();
          if (this.isFinishing()){
              player.stop();
          }
      }
  </code></pre>
  
  <p>Does the same as onPause (and I do understand the actual differences)</p>
  
  <p>EDIT-  ALSO it doesn't seem to matter if player.stop(); is above or below super.onStop(); but it affecting something i cant see, either way, still no SOLUTION :(</p>
  
  <p>ooooo</p>
  
  <p>ooooo</p>
  
  <p>EDIT- ANOTHER OPTION- BUT DOES NOT WORK</p>
  
  <pre><code>public void onUserLeaveHint() {
      player.stop();
      super.onUserLeaveHint();
  }
  </code></pre>
  
  <p>this stops the music when i press HOME but it also stops it when i start new activities :(</p>
  
  <p>EDIT - A VERY COMMON WORK AROUND IVE SEEN MULTIPLE PLACES, but seems to ridiculous to have to do:</p>
  
  <p>essentialy keep count of the number of activities that have been opened and closed (onResume and onPause would prob be best, but that points irrelevant) and when that count reaches 0, stop the background music.  YES that is pretty simple, but why do I have to programmatically do this, actually the BIGGEST QUESTION FOR THIS POST IS:</p>
  
  <h2>WHY DOES PRESSING THE HOME BUTTON NOT CALL onPause or onStop ???</h2>
  
  <p>To put it in onDestroy is not an option because onDestroy is only called when the system is low on memory, or you force close your app, and that is well documented.</p>
  
  <p>Overriding the HOME button also is no option as I have read it's "not possible" and I have read it's "extremely frowned upon", either way I'm avoiding that.</p>
  
  <p>I don't see the need to create SERVICE, and even if I did, when would I stop the service, it seems I would have the same problem</p>
  
  <p>NOW HERE IS THE THING that completely blows my mind, <strong>every</strong> game and <strong>every</strong> app I have downloaded from the android market has very beautiful background music, and when I press BACK or HOME because I am done playing that lovely game <strong>the music stops</strong>, not keeping playing in the background.</p>
  
  <p>I am very frustrated, and I feel it's because I feel like I am missing something <strong>very</strong> simple, because this is one of the most basic lifecycle issues with any app.</p>
  
  <p>I spent a month reading every page of developer.android.com including the Dev Guide, the tutorials, sample projects, and researching the Reference section, as well as google-ing this topic for 5 hours.</p>
  
  <p>I also don't understand why the 6 or 7 SO threads with the <strong>exact</strong> same issue, have <strong>not</strong> been answered, every downloadable app on the market stops playing its music (unless its a background music player) when you press HOME or BACK or whatever to "exit" the app to go do something else on your phone.</p>
  
  <p>What am I missing?</p>