Cancel Method in AsyncTask

Source: Internet
Author: User

Here is a summary:

Document address

Cancelling a task

A task can be canceled at any time by invoking cancel (boolean ). invoking this method will cause subsequent callto 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 canceled as quickly as possible, you shoshould always check the return value of isCancelled () periodically from doInBackground (Object []), if possible (inside a loop for instance .)

This means

We can call cancelboolean at any time) to cancel the loading task. Calling this method indirectly calls iscancelled and returns true.

After cancel () is called, after doInBackground) return, we will call onCancelled (Object) instead of onPostExecute (Object)

To ensure faster task cancellation, you should check iscancelled periodically in doInBackground for judgment.

** Note that our oncancel and onPostExecute are both executed in the UI thread... So when we want to cancel the operation, we can change the UI in oncancel in some interface changes.

Cancel Method

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 canceled, or cocould not be canceled for some other reason. if successful, and this task has not started when cancel is called, this task shoshould never run. if the task has already started, then the mayInterruptIfRunning parameter determines whether the thread executing this task shocould be interrupted in an attempt to stop the task.

In some cases, canceltrue may be invalid.

For example, a task has been loaded, canceled, or otherwise

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.