Concurrent AsyncTask Execution

AsyncTask is executed when clicked:

List<RSSItem> list = new Vector<RSSItem>();
private OnClickListener click = new OnClickListener() {
    public void onClick(View view) {
        list.clear();
        if((dft.getStatus().toString()).equals("RUNNING")) dft.cancel(true);
        currentCategory = catigoriesHolder.indexOfChild(view);
        dft = new DownloadFilesTask();
        dft.execute(rssFeedURL[currentCategory]);
    }
};

In doInBackGround variablefilled out. How to prevent the deletion of a list while it is being used to populate a ListView . How to be sure that the next time you click, the previous AsyncTask instance was destroyed and its further processing will not be processed.

The question is about version 1.6.

0
source share
2 answers

async , async ., , 2 DownloadFilesTask , :

task1.execute();
task2.execute();

, 2 , task1 onPreExecute, DoInBg, onPostExecute, , . taskStatus - ENUM. , , :

task.getStatus()==Status.FINISHED

, , , :

if(task==null || task.getStatus()!=Status.FINISHED){
    task = new DownloadFilesTask();
    task.execute();
}

, doInBackground , postExecute . , "", bg.

+3

AsyncTask -. , :

List<RSSItem> list = new Vector<RSSItem>();
DownloadFilesTask downloadTask = null;

private OnClickListener click = new OnClickListener() {
    public void onClick(View view) {
        list.clear();
        if((dft.getStatus().toString()).equals("RUNNING")) dft.cancel(true);
        currentCategory = catigoriesHolder.indexOfChild(view);

        if(downloadTask == null){
           downloadTask = new DownloadFilesTask();
           downloadTask.execute(rssFeedURL[currentCategory]);
        } else { 
           //show warning here
        }
    }
}

, downloadTask null onPostExecute(), .


, :

 @Override
 onDestroy() {
     if(downloadTask != null) {
         downloadTask.cancel();
     }
 }

.

+2

All Articles