AysncTask cancelling itself still calls onPostExecute()

前端 未结 2 481
攒了一身酷
攒了一身酷 2021-01-02 15:31

After calling AsyncTask.cancel(true) from within doInBackground(), instead of calling onCancelled(), Android calls onPostExecut

2条回答
  •  说谎
    说谎 (楼主)
    2021-01-02 16:15

    1. There is an exception because you call cancel(true) which sends an interrupt to the thread running doInBackground() - however, in this case, you are calling cancel(true) from within doInBackground(), thus causing the thread to immediately send an interrupt to itself.

    2. Your code is running on Android 2 but you are quoting the docs for Android 4. The problem is that the behaviour on cancel() changed between Android 2 and Android 4.

      Android 2.3.7 onPostExecute :

      Runs on the UI thread after doInBackground. The specified result is the value returned by doInBackground or null if the task was cancelled or an exception occured.

      Android 4.0.1 onPostExecute :

      Runs on the UI thread after doInBackground. The specified result is the value returned by doInBackground. This method won't be invoked if the task was cancelled.

提交回复
热议问题