Stop AsyncTask doInBackground method

后端 未结 1 2017
南旧
南旧 2020-11-28 00:03

I have created an AsyncTask in a class and i am calling that task from a fragment. The problem is i want to stop the doInBackground method if the fragment is destroyed. To d

相关标签:
1条回答
  • 2020-11-28 00:34

    public final boolean cancel (boolean mayInterruptIfRunning)

    Attempts to cancel execution of this task. This attempt will fail if the task has already completed, already been cancelled, or could not be cancelled for some other reason. If successful, and this task has not started when cancel is called, this task should never run. If the task has already started, then the mayInterruptIfRunning parameter determines whether the thread executing this task should be interrupted in an attempt to stop the task.

    Use isCancelled()

    public final boolean isCancelled ()

    Returns true if this task was cancelled before it completed normally. If you are calling cancel(boolean) on the task, the value returned by this method should be checked periodically from doInBackground(Object[]) to end the task as soon as possible.

    Android - Cancel AsyncTask Forcefully

    Check the accepted answer and the answer by commonsware in the above link

    0 讨论(0)
提交回复
热议问题