title: Android: Media Player not pausing in second activity using ImageView
body: <p>I have spent almost the day trying to figure it out and yet could not manage to achieve it. I basically have two activities, <code>MainActivity.java</code> and <code>GameActivity.java</code>. The music stops but after going back to <code>MainActivity.java</code> and coming back to <code>GameActivity.java</code> using <code>ImageView</code> several times.</p>
  
  <blockquote>
    <p>MainActivity.java</p>
  </blockquote>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
  ImageView iv;
  public static final String BG_SOUND_CHECK = "background playing!";
  //  public static MediaPlayer backgroundSound;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  final Handler handler = new Handler();
  final MediaPlayer backgroundSound = MediaPlayer.create(this, R.raw.background_music);
  
  
  handler.postDelayed(new Runnable() {
      @Override
      public void run() {
          backgroundSound.setLooping(true);
          backgroundSound.start();
          Log.v(BG_SOUND_CHECK, "After loop Started!");
  
      }
  }, 3000);
  
  
  getSupportActionBar().hide();
  iv = (ImageView)findViewById(R.id.playImage);
  iv.setOnClickListener(new View.OnClickListener(){
      public void onClick(View view1){
  //                backgroundSound.setLooping(false);
                  backgroundSound.pause();
  //                Log.v(BG_SOUND_CHECK, "Playing After Play Button Click");
                  Intent myIntent = new Intent(MainActivity.this, GameActivity.class);
  //         myIntent.putExtra("backgroundSoundObj",backgroundSound);
                  startActivity(myIntent);
  //                Log.v(BG_SOUND_CHECK, "After Game Scene");
  
      }
  });
  
  
   }
  }
  </code></pre>
  
  <blockquote>
    <p>GameActivity.java</p>
  </blockquote>
  
  <pre><code>   public class GameActivity extends AppCompatActivity {
      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_game);
  
      //        MainActivity.backgroundSound.pause();
  
      //   Rest of the code
      }
      }
  </code></pre>
  
  <p>I tried putting the music <code>onPause();</code> in 
  1) ImageView onClick();
  2) As soon as the GameActivity.java is loaded</p>
  
  <p>but both ways end up pausing it only after I try coming back to <code>MainActivity.java</code> and going to <code>GameActivity.java</code> several times.</p>