Asynctask.cancel () The end of the issue

Source: Internet
Author: User

There is a problem in the actual project, the user enters the detail interface, then we need to load the network data and display in the UI, this load with the thread or asynchronous.

In this case, get the network data using the unified asynchronous task in the project.

The user may have such an operation, it in a product (say, etc) list, click on a list item, enter the corresponding detail interface, this time, we will open an asynchronous task to obtain network data, but the network is poor, the user may not be willing to wait, immediately press the back button back to the list, Click on the next list item to enter the details screen, find the load is too slow, and press the back key, so repeatedly, so that there are more than one asynchronous task at this time to execute, or an oom problem, or an asynchronous task waiting problem.

So, as a developer, our solution is to end an asynchronous task that is not finished when the user returns to the previous interface by pressing the exit button on the details screen.

--------------------------------------------------------------------------------------------------------------- ---------------

Now we have found the problem, and we have a solution. Then it is implemented in code.

So how do I cancel an asynchronous task?

I started doing this, asynctask.cancel (true);

Look at the definition of the parameter:

@param mayinterruptifrunning <tt>trueifthis     * in        -Progress Tasks is allowed     * to complete        .

1, if True, if the thread executes, it will be interrupted

2, if it is false, the thread will be run execution complete

See this, obviously, we thought. Cancel (true) ends the asynchronous task that we opened

But it doesn't actually end the asynchronous task we want to end.

Read some other people's explanations for this:

Asynctask does not directly end a thread without considering the result. Calling Cancel () is actually setting a "canceled" state for Asynctask. It's up to you to check if Asynctask has been canceled, and then decide whether or not to terminate your operation. For mayinterruptifrunning--it does just make a interrupt () call to a running thread. In this case, your thread is non-interruptible and will not terminate the thread.

So how do you end the thread?

Visible. Cancel () is a state that sets a "canceled" for Asynctask, so if you want to terminate an asynchronous task, you need to end it in an asynchronous task.

@Override Public voidonprogressupdate (Integer ... value) {//decide whether to be canceledif(IsCancelled ())return;.........} @OverrideprotectedInteger doinbackground (Void ... mgs) {//The task is canceled, exit immediately.if(IsCancelled ())return NULL;.......//The task is canceled, exit immediately.if(IsCancelled ())return NULL;} ...

Additional conditions to end the asynchronous task:

if (loadasyncvedio!=null &&!) loadasyncvedio.iscancelled ()                     && loadasyncvedio.getstatus () = = AsyncTask.Status.RUNNING) {                loadasyncvedio.cancel (True );                 NULL ;            }
Loadasyncvedio (asynchronous Task)

This way, you can effectively and timely end the asynchronous task

Asynctask.cancel () The end of the issue

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.