Declaring AsyncTask types
AsyncTask
is a generically typed class, and exposes three type parameters:
abstract class AsyncTask<Params, Progress, Result>
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:
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 representing the void
keyword.
Let's take a look at a first example, ...
Get Asynchronous Android now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.