title: Stop/start mediaplayer from another activity
body: <p>i'm trying to play <strong>stream radio</strong> using <code>Mediaplayer</code> with <strong>MP1</strong> as variable of <strong>Mediaplayer</strong> i want to play it in all <code>Fragments</code> app,expect one <code>activity</code> (<strong>ActivityOne</strong>)  which is contains another <code>Mediaplayer</code> <strong>MP2</strong> to play,so i want to stop <strong>MP1</strong> when i'm in (<strong>ActivityOne</strong>) <code>activity</code>, and play <strong>MP2</strong> , and when i return from (<strong>ActivityOne</strong>) i want to resume <strong>MP1</strong>, my big problem is the (<strong>ActivityOne</strong>) called when i click <code>button</code> which is exist in <code>fragment</code>
  my code below works only in one direction : 
  when i return from (<strong>ActivityOne</strong>) activity, the music stops.</p>
  
  <p>structure of the app : <strong>MainAcitivty</strong> > <strong>Fragment</strong> > <strong>ActivityOne</strong></p>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>MediaPlayer MP1;
  boolean prepared = false;
  boolean started = false;
  PlayerTask playerTask;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      mediaPlayer = new MediaPlayer();
      playerTask = new PlayerTask();
      playerTask.execute(stream);
      /**/
         MusicButton = findViewById(R.id.toggleButton);
      MusicButton.setVisibility(View.INVISIBLE);
      MusicButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
          @Override
          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
              if (started &amp;&amp; MusicButton.isChecked()) {
                  started = false;
                  MP1.pause();
                  MusicButton.setChecked(true);
              } else {
                  started = true;
                  MP1.start();
                  MusicButton.setChecked(false);
              }
          }
      });
  }
  @SuppressLint("StaticFieldLeak")
  public class PlayerTask extends AsyncTask&lt;String, Void, Boolean&gt; {
      ProgressBar loadingRL = findViewById(R.id.progressBar);
      @Override
      protected void onPreExecute() {
          super.onPreExecute();
          if (android.os.Build.VERSION.SDK_INT &gt;= android.os.Build.VERSION_CODES.LOLLIPOP) {
              AudioAttributes attribs = new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA).setContentType(AudioAttributes.CONTENT_TYPE_MUSIC).build();
              MP1.setAudioAttributes(attribs);
  
          } else {
              MP1.setAudioStreamType(AudioManager.STREAM_MUSIC);
          }
          loadingRL.setVisibility(View.VISIBLE);
  
      }
  
      @Override
      protected Boolean doInBackground(String... strings) {
          try {
              MP1.setDataSource(strings[0]);
              MP1.prepare();
              prepared = true;
          } catch (IOException e) {
              e.printStackTrace();
          } catch (IllegalArgumentException e) {
              e.printStackTrace();
          } catch (IllegalStateException e) {
              e.printStackTrace();
          }
          MP1.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
              @Override
              public void onPrepared(MediaPlayer MP1) {
                  MP1.start();
  
              }
          });
          return prepared;
      }
  
      @Override
      protected void onPostExecute(Boolean aBoolean) {
          super.onPostExecute(aBoolean);
          MusicButton.setVisibility(View.VISIBLE);
          MusicButton.setChecked(true);
           loadingRL.setVisibility(View.VISIBLE);
  }
  </code></pre>
  
  <p><strong>ActivityOne.java</strong></p>
  
  <pre><code>    MediaPlayer MP2;
      boolean prepared = false;
      boolean started = false;
      ToggleButton music;
      PlayerTask playerTask = null;
      CoordinatorLayout coordinatorLayout;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_pop_for_ringtone);
          coordinatorLayout = findViewById(R.id.coord);
          MP2 = new MediaPlayer();
          playerTask = new PlayerTask();
          playerTask.execute(url);
  }
    @SuppressLint("StaticFieldLeak")
      public class PlayerTask extends AsyncTask&lt;String, Void, Boolean&gt; {
          ProgressBar pb = findViewById(R.id.progress);
  
          @Override
          protected void onPreExecute() {
              super.onPreExecute();
              if (android.os.Build.VERSION.SDK_INT &gt;= android.os.Build.VERSION_CODES.LOLLIPOP) {
                  AudioAttributes attribs = new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA).setContentType(AudioAttributes.CONTENT_TYPE_MUSIC).build();
                  MP2.setAudioAttributes(attribs);
  
              } else {
                  MP2.setAudioStreamType(AudioManager.STREAM_MUSIC);
              }
          }
  
          @Override
          protected Boolean doInBackground(String... strings) {
              if (!isCancelled()) {
                  try {
                      MP2.setDataSource(strings[0]);
                      MP2.prepare();
                      prepared = true;
                  } catch (IOException e) {
                      e.printStackTrace();
                  } catch (IllegalArgumentException e) {
                      e.printStackTrace();
                  } catch (IllegalStateException e) {
                      e.printStackTrace();
                  }
                  MP2.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                      @Override
                      public void onPrepared(MediaPlayer MP2) {
                          MP2.start();
                      }
                  });
  
              }
              return prepared;
          }
  
          @Override
          protected void onPostExecute(Boolean aBoolean) {
              super.onPostExecute(aBoolean);
              music.setEnabled(true);
              music.setVisibility(View.VISIBLE);
              music.setChecked(true);
              all.setVisibility(View.VISIBLE);
          }
  
          @Override
          protected void onCancelled(Boolean aBoolean) {
              if (isCancelled() &amp;&amp; MP2.isPlaying()) {
                  MP2.stop();
              }
          }
      }
  
   @Override
      public void onBackPressed() {
          if (playerTask != null &amp;&amp; playerTask.getStatus() == AsyncTask.Status.FINISHED) {
              if (MP2.isPlaying()) {
                  MP2.stop();
              }
          } else if (playerTask != null &amp;&amp; playerTask.getStatus() != AsyncTask.Status.FINISHED) {
              playerTask.cancel(true);
          }
          super.onBackPressed();
      }
  </code></pre>
  
  <p>i spent 2 days to resolve this problem without any result ,please someone help me i will be thankful to him</p>