title: Stop AsyncTask when fragment is replaced
body: <p>In my fragment I have 2 AsyncTasks continuously running. The requirement is that when the user switches to a different frame using the Drawer menu, these tasks should be cancelled. However when the user presses the home button, these tasks should keep running.</p>
  
  <p>I used the following code in that fragment:</p>
  
  <pre><code>@Override
  public void onStop() {
      super.onStop();
  
      if(asyncTripWaiter != null &amp;&amp; asyncTripWaiter.getStatus() == AsyncTask.Status.RUNNING)
          asyncTripWaiter.cancel(true);
  
      if(asyncTripTracker != null &amp;&amp; asyncTripTracker.getStatus() == AsyncTask.Status.RUNNING)
          asyncTripTracker.cancel(true);
  } 
  </code></pre>
  
  <p>The problem is that this piece of code executes even when the home button is pressed. How can I make it effective only when the fragment is switched.</p>