3

I'm having trouble understanding the use of the parameters for Asynctask in android.

The Android Developers documentation explains it as follows:

AsyncTask must be subclassed to be used.
 The subclass will override at least one method (doInBackground(Params...)),
   and most often will override a second one (onPostExecute(Result).)

Here is an example of subclassing:

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
    protected Long doInBackground(URL... urls) {
        int count = urls.length;
        long totalSize = 0;
        for (int i = 0; i < count; i++) {
            totalSize += Downloader.downloadFile(urls[i]);
            publishProgress((int) ((i / (float) count) * 100));
            // Escape early if cancel() is called
            if (isCancelled()) break;
        }
        return totalSize;
    }

    protected void onProgressUpdate(Integer... progress) {
        setProgressPercent(progress[0]);
    }

    protected void onPostExecute(Long result) {
        showDialog("Downloaded " + result + " bytes");
    }
}

Once created, a task is executed very simply:

new DownloadFilesTask().execute(url1, url2, url3);

For my extension of AsyncTask, I don't need to pass in any parameters, but I need to override the doInBackground(), onProgressUpdate(), and onPostExecute(). Why do I have to insert Void,Void,Void into AsyncTask<>?

What do the parameters do?

Sufian
  • 6,405
  • 16
  • 66
  • 120
waylonion
  • 6,866
  • 8
  • 51
  • 92
  • This might be helpful for those who come across this question - [What arguments are passed into AsyncTask?](http://stackoverflow.com/questions/6053602/what-arguments-are-passed-into-asynctaskarg1-arg2-arg3) – Sufian Jul 13 '16 at 13:52

3 Answers3

1

Well Async task parameters are simple,

First param : Array of object or single object which needed for background process.

Second param : Object type you are going to pass into to the onProgressUpdate

Third Param : The return type of doInBackground

example :-

private class ImageDownloader extends AsyncTask<Void, Void, Void> {//todo}

ImageDownloader downloader = new ImageDownloader();
downloader.execute();
Zumry Mohamed
  • 9,318
  • 5
  • 46
  • 51
1

From the documentation it says using Void simply marks the type as unused. You don't have to have a type in AsyncTask.

The three types used by an asynchronous task are the following:

  1. Params, the type of the parameters sent to the task upon execution.
  2. Progress, the type of the progress units published during the background computation.
  3. Result, the type of the result of the background computation.

Not all types are always used by an asynchronous task. To mark a type as unused, simply use the type Void:

private class MyTask extends AsyncTask<Void, Void, Void> { ... }
Sufian
  • 6,405
  • 16
  • 66
  • 120
JRowan
  • 6,824
  • 8
  • 40
  • 59
1

You insert void void void if you have no need for the parameters. For example when the async task is done and in onPostExecute you just want to update the view and it isn't reliant on the result doInBackground you can put Void as the 3rd parameter.

I think it is preferred practice to use parameters however instead of holding variables in either the async task or instance variables if the async task is a private inner class.

Dom
  • 8,222
  • 2
  • 15
  • 19