Declaring AsyncTask types
AsyncTask
is a generically typed class that exposes three generic type parameters:
abstract class AsyncTask<Params, Progress, Result>
In order to use a generic type, we must provide one type argument per type parameter that was declared for the generic type.
Note
The generic type class provides a way to re-use the same generic algorithms for different input types. A generic type could have one or more type parameters.
When we declare an AsyncTask
subclass, we'll specify the types for Params, Progress, and Result; for example, if we want to pass a String
parameter to doInBackground
, report progress as a Float
, and return a Boolean
result, we would declare our AsyncTask
subclass as follows:
public class MyTask extends AsyncTask<String, Float, Boolean>
If we don't need to pass any parameters, or don't want to report progress, a good type to use for those parameters is java.lang.Void
, which signals our intent clearly, because Void
is an uninstantiable class...