Making use of the Android AsyncTask class

Prateek Mishra
Nybles
Published in
3 min readJun 12, 2019
Image from: 9to5google.com

No matter where your attachments lie, we all hate lag on devices with a vengeance. Android devices are regular victims of unspeakable lag although there are some good practices that developers must follow for smooth functioning apps.

Let’s say you need a feature implementing some networking, file and database access, or complex calculations. These are heavy operations and will definitely result in an unsought anguish. Users tolerate some pauses, but a frozen app just drives them nuts!

All android applications have a main thread, a thread is some instructions executing independently. This thread is responsible for the user interface. If complex code is executed on this thread it may get held up and result in the app slowing down, freezing or even crashing entirely! It is therefore recommended to execute lengthy instructions in background threads. The simplest way to perform background operations is the AsyncTask class.

This class allows you to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers. An asynchronous task is defined by a computation that runs in the background thread and whose result is published on the UI thread. But how does this class communicate with the activity this is put on?

To use Async Task, simply declare a new class, make it extend the AsyncTask class with the 3 parameters as discussed below.

public class Asynctaskexample extends AsyncTask<Integer, Integer, String>{
@Override
protected void onPreExecute() {
super.onPreExecute();
}

@Override
protected String doInBackground(Integer... integers) {
return null;
}


@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
}

@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
}
}

An asynchronous task is defined by 3 generic types, called Params, Progress and Result (here Integer, Integer and String respectively). Any class that extends AsyncTask has these parameters. Params is what the user passes into this Async task class, this value goes into the doInBackground method. It may be a url or the numbers for large calculation, any data which has to be processed in the background. Progress is the variable used to keep track of the progress. For example this may be the index of some large loop which can be used to show progress using some progress dialogue. Result is what is returned from the doInBackground method to the onPostExecute method eg some string or anything else. As can be seen an Async Task has the following four major methods, override the following four methods.

  1. onPreExecute: a step used to set up the task. Here execute something that you want to be executed before the lengthy process.
  2. doInBackground: a step used to perform the actual task. This creates a background thread and does the job.
  3. onProgressUpdate: a step used to update the current progress status of the task that is being performed in doInBackground.
  4. onPostExecute: once doInBackground finishes executing the task, this step delivers the result back to the main UI thread and stops the AsyncTask process

It’s important to note that the onPreExecute and the onPostExecute methods run on the UI thread i.e. they do not run in the background. doInBackground runs on a new thread in the background and you cannot update any graphics in the interface that the user sees, however the onProgressUpdate can be used a rescue.

That brings us to an end in exploring the AsyncTask. Hope you guys learnt something and enjoyed this too! If you liked this blog show some ❤, stay tuned for more awesome content.

Interested in learning more about AsyncTask class and how background threads work? Check out these Android developer resources.

  1. https://developer.android.com/reference/android/os/AsyncTask.html
  2. https://www.udemy.com/learn-android-application-development-y/learn/lecture/7811358#overview

About Me: I am an Android developer at IIIT-Allahabad. I love solving real world problems, creative solutions is my where my heart lies!

--

--

Prateek Mishra
Nybles
Writer for

I am a passionate software engineer with a strong foundation in computer science. When I am not working, you can find me by the swimming pool or reading books.