title: Stop MediaPlayer when Fragment closes
body: <p>I am using a MediaPlayer inside a Fragment, I have managed to stop the music from playing, whenever the current fragment is changed. </p>
  
  <p>But the problem is that, if I was within the fragment, and I press the back button to close the application, the music keeps streaming.</p>
  
  <p>My code is the following: </p>
  
  <pre><code>@Override
  
  public void setUserVisibleHint (boolean isVisibleToUser){
  
  
  super.setUserVisibleHint(isVisibleToUser);
  
  If (this.isVisible ()){
  
  If (!isVisibleToUser){
  
  sound.stop ();
  
  }else {
  
  sound.start ();
  
  }
  
  }
  
  }
  
  sound = MediaPlayer.create(getActivity
  
  (), R.raw.music);
  </code></pre>
  
  <p>How can I stop the music from playing, when I exit the application?</p>