title: Destroy/Stop MediaPlayer when switching fragment
body: <p>I have a tablet view with a side pannel and the main fragment to the right. Issue is I have a MediaPlayer that continues playing even when someone switches to another fragment, this means when the user goes back onto the fragment, it cause crashes with the pause button. How would I make it so once a user press another fragment, it stop/destroys the MediaPlayer?</p>
  
  <pre><code>public class Fragment_7 extends Fragment {
  
  private MediaPlayer mp;
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
  
  {
  
      return inflater.inflate(R.layout.fragment_7, null);
  }
  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
      super.onActivityCreated(savedInstanceState);
  
  
  
  
  
      GridView gridView = (GridView)getView().findViewById(R.id.gridview);
      gridView.setAdapter(new MyAdapter(getActivity()));
  
      gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  
          @Override
          public void onItemClick(AdapterView&lt;?&gt; parent, View view,
                                  int position, long id) {
  
              switch (position) {
                  case 0:
                      stopPlaying();
                      mp = MediaPlayer.create(getActivity(), R.raw.sound01);
                      mp.start();
  
                      // This will make  sound
                      break;
                  case 1:
                      stopPlaying();
                      mp = MediaPlayer.create(getActivity(), R.raw.sound02);
                      mp.start();
                      break;
                  case 2:
  
                          stopPlaying();
                          mp = MediaPlayer.create(getActivity(), R.raw.sound03);
                          mp.start();
                      break;
                  case 3:
                      stopPlaying();
                      mp = MediaPlayer.create(getActivity(), R.raw.sound04);
                      mp.start();
                      break;
                  case 4:
                      stopPlaying();
                      mp = MediaPlayer.create(getActivity(), R.raw.sound05);
                      mp.start();
                      break;
                  case 5:
                      Intent i = new Intent(Intent.ACTION_VIEW,
                              Uri.parse("https://play.google.com/store"));
                      startActivity(i);
  
                      break;
              }
          }
      });
              FloatingActionButton fab = (FloatingActionButton) getView().findViewById(R.id.fab);
  
              fab.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      if (mp.isPlaying()) {
                          mp.pause();
                          mp.seekTo(0);
                      }
                  }
              });
          }
  
  
      private void stopPlaying() {
      if (mp != null) {
          mp.stop();
          mp.release();
          mp = null;
      }}}
  </code></pre>