parallel execution of AsyncTask

后端 未结 2 1073
南方客
南方客 2021-01-28 11:14

An AsyncTask is executed on click:

List list = new Vector();
private OnClickListener click = new OnClickL         


        
2条回答
  •  情话喂你
    2021-01-28 11:58

    You can store reference to AsyncTask in member variable. So your code would look like this:

    List list = new Vector();
    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
            }
        }
    }
    

    Of course, you'll need to set downloadTask to null in onPostExecute() for this to work.


    As an added benefit you now can cancel outstanding task if Activity is being destroyed:

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

    Which you should do anyway.

提交回复
热议问题