AsyncTask. cancel () end problem, asynctaskcancel
There is such a problem in the actual project. When the user enters the details page, we need to load data on the network and display the data on the UI. This loading uses threads or Asynchronization.
Here, we use Asynchronous tasks in the project to obtain network data.
The user may have such an operation. It is in a commodity (talk about) List, click a list item to enter the corresponding details interface. At this time, we will enable an asynchronous task to obtain network data. However, when the network is poor, users may not want to wait. Immediately press the back button to return to the list, click the next item in the list to go to the details page. It is found that loading is too slow and you press the back key again and again. As a result, multiple asynchronous tasks are being executed or an OOM problem occurs, or an asynchronous task wait problem occurs.
As a developer, the corresponding solution is to end the unfinished asynchronous task when the user returns to the previous interface by pressing the exit button on the details page.
Bytes ------------------------------------------------------------------------------------------------------------------------------
Now we have found this problem and a solution. The code is used for implementation.
How can I cancel an asynchronous task?
In the first step, AsyncTask. cancel (true );
Let's take a look at the parameter definitions:
@param mayInterruptIfRunning <tt>true</tt> if the thread executing this * task should be interrupted; otherwise, in-progress tasks are allowed * to complete.
1. If it is true, it will be interrupted if the thread is executed.
2. If it is false, the thread will be executed completely.
Obviously, we thought that. cancel (true) would end the ongoing asynchronous task we enabled.
However, the asynchronous task we want to end is not actually completed ~~
I read some other people's explanations:
AsyncTask does not end a thread directly without considering the results. Cancel () is used to set a "canceled" status for AsyncTask. It depends on whether you check whether the AsyncTask has been canceled and then decide whether to terminate your operation. For mayInterruptIfRunning -- all it does is send an interrupt () call to the running thread. In this case, your thread cannot be interrupted, and it will not be terminated.
So how to end the thread?
It can be seen that cancel () sets a "canceled" state for AsyncTask. to terminate an asynchronous task, it must end in the asynchronous task.
@ Overridepublic void onProgressUpdate (Integer... value) {// determine whether the request is canceled if (isCancelled () return ;.........} @ Overrideprotected Integer doInBackground (Void... mgs) {// The Task is canceled. immediately exit if (isCancelled () return null ;....... // if (isCancelled () return null ;}...
In addition, the conditions for stopping an asynchronous task are as follows:
if(loadAsyncVedio!=null && !loadAsyncVedio.isCancelled() && loadAsyncVedio.getStatus() == AsyncTask.Status.RUNNING){ loadAsyncVedio.cancel(true); loadAsyncVedio = null; }
LoadAsyncVedio (asynchronous task)
In this way, you can end asynchronous tasks in a timely and effective manner.