Declaring AsyncTask types
AsyncTask
is a generically typed class, and exposes three type parameters:
When we declare an AsyncTask
subclass, we'll specify 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:
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 representing the void
keyword.
Let's take a look at a first example, performing an expensive calculation in the background and reporting the result to the main thread: