title: How to stop activity in Android?
body: <p>I have created an activity which includes media player in it.When I start activity, the music begins to play.But when the song is complete and when i click on back button on the emulator, it displays an error of (<code>IllegellStateException</code>).</p>
  <p>Here is my code:</p>
  <pre><code>public void onCreate(Bundle savedInstanceState)
  {
      super.onCreate(savedInstanceState);
              
      setContentView(R.layout.audio);
      init();
      imgVw.setImageResource(R.raw.teddy_two);
      
      prefs = PreferenceManager.getDefaultSharedPreferences(this);
        
      mp=MediaPlayer.create(Audio_Activity.this,R.raw.ennamo_yadho);
      Log.e(&quot;Song is playing&quot;,&quot;in  Mediya Player &quot;);
      Log.e(&quot;Current &quot;,&quot;Position -&gt; &quot; + length);
      mp.setLooping(false);
      mp.start();
      btnChapter.setEnabled(false);
              
      mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() 
      {
          @Override
          public void onCompletion(MediaPlayer mp) 
          {
              // TODO Auto-generated method stub
              mp.stop();
              mp.release();
              btnChapter.setEnabled(true);
              System.out.println(&quot;Music is over and Button is enable !!!!!!&quot;);
          }
      });
              
      @Override
      public void onConfigurationChanged(Configuration newConfig) {
          super.onConfigurationChanged(newConfig);
           
          // Checks the orientation of the screen
          if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
              Toast.makeText(this, &quot;landscape&quot;, Toast.LENGTH_SHORT).show();
          } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
              Toast.makeText(this, &quot;portrait&quot;, Toast.LENGTH_SHORT).show();
          }
      }
           
      @Override
      public void onPause()
      {
          super.onStop();
          SharedPreferences. Editor prefsEdit = prefs.edit();
                  
          int position = mp.getCurrentPosition();
          prefsEdit.putInt(&quot;mediaPosition&quot;, position);
          prefsEdit.commit();
      }
      
      @Override
      protected void onResume() 
      {
          super.onResume();
          System.out.println(&quot;Activity is Resume !!!&quot;);
          int position = prefs.getInt(&quot;mediaPosition&quot;, 0);
          mp.seekTo(position);
          mp.start();
      }
          
      #Override
      public boolean onKeyDown(int keyCode, KeyEvent event) 
      {
          if ((keyCode == KeyEvent.KEYCODE_BACK)) 
          { 
              if(mp!= null)
              {
                  mp.pause();
              }
              //finish();
           return true;
       }
       return super.onKeyDown(keyCode, event);
   }
  </code></pre>
  <p>}</p>