A task can be cancelled at any time by invoking cancel(boolean). Invoking this method will cause subsequent calls to isCancelled() to return true. After invoking this method, onCancelled(Object), instead of onPostExecute(Object) will be invoked after doInBackground(Object[]) returns. To ensure that a task is cancelled as quickly as possible, you should always check the return value of isCancelled() periodically from doInBackground(Object[]), if possible (inside a loop for instance.)
For further details visit:
https://developer.android.com/reference/android/os/AsyncTask.html
Just like:
onPostExecute(Result result)
{
}
you can override a method for cancel to update your UI accordingly when your asyncTask is cancelled as follows:
onCancelled(Result result)
{
// here you can handle situation according to your requirements
}
So point of your interest here is that whenever you cancel your task before it enters into onPostExecute it will not go into onPostExecute but will jump to onCancelled. You can also call task.cancel(true); in postExecute according to your condition by applying required checks.