title: how do i stop background music when back button is pressed?
body: <p>i have 2 activities. and 2 different background music plays on both. when i press a button on the first activity it will go to this activity and the changing of music goes smoothly but i want to stop background music when back button is pressed. i tried this but it force closes. is there any other way to do this?</p>
  
  <pre><code>public class Categories extends Activity{
  
  MediaPlayer mp;
  @Override
  protected void onCreate(Bundle savedInstanceState){
      super.onCreate(savedInstanceState);
      setContentView(R.layout.categories);
  
      mp = MediaPlayer.create(Categories.this, R.raw.looney);
      mp.setLooping(true);         
      mp.start();
  
      Button cartoonButton = (Button) findViewById(R.id.cartoon);             
      cartoonButton.setOnClickListener(new View.OnClickListener() {           
          public void onClick(View v) {
              Intent launchCartoon = new Intent(Categories.this, Cartoon.class);
              startActivity(launchCartoon);   
  
          }
      }); 
  
  
      Button superButton = (Button) findViewById(R.id.hero);
      superButton.setOnClickListener(new View.OnClickListener() 
      {           
          public void onClick(View v) {
              Intent launchSuperheroes = new Intent(Categories.this, SuperHeroes.class);
              startActivity(launchSuperheroes);
  
          }
      });
  
      Button singerButton = (Button) findViewById(R.id.singer);
      singerButton.setOnClickListener(new View.OnClickListener() {            
          public void onClick(View v) {
              Intent launchSinger = new Intent(Categories.this, Singer.class);
              startActivity(launchSinger);
  
          }
      });
  
      Button famousButton = (Button) findViewById(R.id.famous);
      famousButton.setOnClickListener(new View.OnClickListener() {
  
          public void onClick(View v) {
              Intent launchFamous = new Intent(Categories.this, Famous.class);
              startActivity(launchFamous);
  
          }
      });
  }
  
  @Override
  protected void onResume() {
      super.onResume();
      mp.start();
  }
  
  @Override
  protected void onPause() {
      super.onPause();
      if (mp != null) {
          mp.stop();
          mp.release();
          mp = null;
      }
  }
  </code></pre>
  
  <p>}</p>