i have an activities that host fragments. pressing a button goes from fragment A to fragment B through a FragmentTransaction and the added to the back stack. Now fragment B
Just call this code in the onBackPressed()-method of your hosting Activity - same applies to a Fragment via onDestroy():
if(myFancyAsyncTask != null && myFancyAsyncTask.getStatus() == Status.RUNNING) {
myFancyAsyncTask.cancel(true);
}
This will cancel a running AsyncTask for you. Be sure to check back in the AsyncTask if it was not cancelled by using the lifecycle-methods:
protected void onPostExecute(Long result) {
if(!isCancelled()) {
// do your work
}
}