The Android documentation has an example for using AsyncTask, in this example the DownloadFilesTask class extends AsyncTask in a rather odd way (for a beginners perspective at least):
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
Shouldn’t AsyncTask be accepting type variables instead of primitive types? And what happens if I want to input strings in all 3 parameters? Like so:
private class DownloadFilesTask extends AsyncTask<String, String, String> {
How does the class then reference these parameters individually?
Those generic typs only define what kind of input types you have for
<Param, Progress, Result>meaning that all the params toexecute()are of typeParam, those foronProgressUpdateare ofProgressand thatdoInBackground()will return something of typeResult. The generics only define the type of stuff that is expected later.If you have a decent IDE, when you define the
class X extends AsyncTask<...>it will provide you with the callbacks where all the right types are filled in.In your 2nd example — that does not mean that
execute()expects 3 Strings – actually it expects one or more variables of typeParam, as it is a varargs signature. Within the method, you would then reference the individiual instances like accessing an array.