AsyncTask: where does the return value of doInBackground() go?

Dhruv Mevada picture Dhruv Mevada · Dec 20, 2010 · Viewed 91k times · Source

When calling AsyncTask<Integer,Integer,Boolean>, where is the return value of:

protected Boolean doInBackground(Integer... params)?

Usually we start AsyncTask with new AsyncTaskClassName().execute(param1,param2......); but it doesn't appear to return a value.

Where can the return value of doInBackground() be found?

Answer

Konstantin Burov picture Konstantin Burov · Dec 20, 2010

The value is then available in onPostExecute which you may want to override in order to work with the result.

Here is example code snippet from Google's docs:

 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));
          }
          return totalSize;
      }

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

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