Skip to content

Instantly share code, notes, and snippets.

@alshell7
Last active June 17, 2016 19:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alshell7/95845157525d3e69e54b681a6694e775 to your computer and use it in GitHub Desktop.
Save alshell7/95845157525d3e69e54b681a6694e775 to your computer and use it in GitHub Desktop.
Androids AsyncTask Explanation

##Basically AsyncTask uses generic types in its parameters 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: private class MyTask extends AsyncTask<Void, Void, Void> { ... }

##Taking a very known and common example..

 private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
     protected Long doInBackground(URL... urls) {
         int count = urls.length;
         long totalSize = 0;
         for (int i = 0; i < count; i++) {
             totalSize += Downloader.downloadFile(urls[i]);
             publishProgress((int) ((i / (float) count) * 100));
             // Escape early if cancel() is called
             if (isCancelled()) break;
         }
         return totalSize;
     }

     protected void onProgressUpdate(Integer... progress) {
         setProgressPercent(progress[0]);
     }

     protected void onPostExecute(Long result) {
         showDialog("Downloaded " + result + " bytes");
     }
 }

 new DownloadFilesTask().execute(url1, url2, url3);

##How I Grasped AsyncTask in.. Amongst 3 params <generic, generic, generic>, the first generic is the one taken as parameter (array of parameters, called as Varargs) that is used to perform the background operation of AsyncTask. Basically its the parameter that is the most primary, like as a input to a calculation, and calculation/manipulation takes place in doInBackground(generic...) method. And the second generic (Varargs), describes in what way to show the progress of calculation/manipulation,. And that progress is let for that AsyncTask to know by its public method called publishProgress(Object), which triggers the protected void onProgressUpdate(generic...) method, thats worked on the UI thread. And finally, when doInBackground finishes executing statements under it, it returns a value, and that value is given as parameter to its (AsyncTasks) private void method, onPostExecute(generic...), which is executed over the UI thread again, for the given type of third generic as param (got from doInBackground()) in AsyncTask.

##But there are few rules while to follow, that I found from API documentation

  • The AsyncTask class must be loaded on the UI thread. This is done automatically as of JELLY_BEAN.
  • The task instance must be created on the UI thread.
  • execute(Params...) must be invoked on the UI thread.
  • Do not call onPreExecute(), onPostExecute(Result), doInBackground(Params...), onProgressUpdate(Progress...) manually.
  • The task can be executed only once (an exception will be thrown if a second execution is attempted.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment