1

What does this part of the code below <String, Void, Bitmap> mean? I don't even know what this syntax is even called.

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {

}



Here is the original code (Found from here: http://developer.android.com/guide/components/processes-and-threads.html):

public void onClick(View v) {
    new DownloadImageTask().execute("http://example.com/image.png");
}

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    /** The system calls this to perform work in a worker thread and
      * delivers it the parameters given to AsyncTask.execute() */
    protected Bitmap doInBackground(String... urls) {
        return loadImageFromNetwork(urls[0]);
    }

    /** The system calls this to perform work in the UI thread and delivers
      * the result from doInBackground() */
    protected void onPostExecute(Bitmap result) {
        mImageView.setImageBitmap(result);
    }
}
Yaqub Ahmad
  • 27,569
  • 23
  • 102
  • 149
Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338

3 Answers3

6
AsyncTask<String, Void, Bitmap>

Tells AsyncTask is described by 3 distinct types, String as first parameter, Void as second parameter and Bitmap as third parameter, when you use AsyncTask.

This is called Generics in java, introduced from Java5 onwards. Please read this tutorial to understand more about Generics. Here is javadoc on how android AsyncTasktask uses generics.

Update: From AsyncTask javadoc

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.
kosa
  • 65,990
  • 13
  • 130
  • 167
  • That's not entirely accurate, the answer below is more accurate. The three arguments are for type for execution (String), type for publishing progress (Void/none), type for result (Bitmap). – debracey Nov 05 '12 at 18:49
  • @debracey: Which part is not accurate? I think my description is almost exactly same as your description and added link to AsyncTask. Just updated with what 1,2,3 stands also. – kosa Nov 05 '12 at 18:52
  • The phrasing of your first sentence makes it sound like AsyncTask is a method that takes three parameters, particularly the word 'invoke.' That is what makes it feel inaccurate. – Affe Nov 05 '12 at 18:56
  • @Affe: Changed invoke to use, does it make sense now? – kosa Nov 05 '12 at 18:58
  • It's not really 'accepting parameters' as @Affe points out it makes it sound like it's a method call, which it is not. A better wording might be "comprised by" or "is described by 3 distinct types" -- something like that. It's a minor thing, I think your clarification makes it fine. – debracey Nov 05 '12 at 20:55
  • @debracey: I am not native english speaker, so, happy to update with valid words. Updated answer with described by, which makes more sense. – kosa Nov 05 '12 at 20:58
2

It's called Generics. Here is the details from the AsyncTask manual:

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

Params, the type of the parameters sent to the task upon execution.

Progress, the type of the progress units published during the background computation.

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:

So AsyncTask<String, Void, Bitmap> means that, AsyncTask --DownloadImageTask accepts the param as String, Progress type is unused and returns the result as Bitmap

Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
0

AsyncTask is a generic class. You should look at the generics tutorial to understand the syntax and semantics of generics.

If you look at the AsyncTask docs you will see what each of those parameters means.

  • The first is marked as "params" and is the type that your doInBackground method accepts.
  • The second is the type that is used to denote progress, as taken in the onProgressUpdate method.
  • The third is the resulting type of the task, the type that is returned from doInBackground and received by onPostExecute.
digitaljoel
  • 26,265
  • 15
  • 89
  • 115