In this blog series which is a part of Roadway to Android Development Course, i want to introduced you to one of the most important concept in Android app development and which is Android AsyncTask. AsyncTask is an android class that allows user to interact with their app while some background process like downloading a file or fetching some data from the server is going on. Lets explore this thing in a little detail here.
What is Android AsyncTask?
Suppose you just downloaded a Wallpaper app from Google Play Store and as soon as you open up the app,You see a beautiful welcoming splash screen and then few UI Buttons with label as Car Wallpaper, Cat Wallpaper, City Wallpaper etc. so what ever interaction that you are doing with your app currently is happening on a UI Thread also known as the main thread which processes every task related with user interaction promptly.
Now you clicked on a button labeled ‘City Wallpaper’ and then you noticed a process dialog box moving in circles and waiting to download images from the server while you are sitting on your couch waiting for it to download asap so that you can also send a Whatsapp message to your friend. I hope you too will get impatient just like me.
What can you do if this was your app? Well in such cases its good to use Asynctask.
Android AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.
So what happens in the app activity background is that while you may be busy browsing and looking at some pics in the Wallpaper app main UI Thread, AsyncTask get another thread to work that’ll download more images for you to browse easily and prevents app from freezing like it happened earlier.
In order to implement Asynctask class in your Project you have to extend AsyncTask and must at least override doInBackground() method in your app. Few other common methods that you can override to use some more functionality of AsyncTask are:
This method is called in the main UI Thread before starting up the another new background process thread. The reason to call this method is to display a toast message or a progress bar that Download has started or ‘Downloading image’.
As the name itself explains, In this method you can write all that code which you want to run in the background. This method is called immediately after onPreExecute() method and shares its final output with onPostExecute() Method.
In this method you can write code that you want to run while the process is running in the background.
This method is called after doInBackground() method gets over and takes its output. Here you receive the data from doInBackground() and display it in the main activity thread. Remember that the data type returned by doInBackground() should match with data type sent by onPostExecute() Method.
Android AsyncTasks is a generic class, meaning it enable Android programmers to specify, with a single method declaration, a set of related methods. It uses three types:
AsyncTask<Params, Progress, Result>
- Params is the input that you give to AsyncTask.
- Progress is used if you’ve any updates sent to onProgressUpdate() method.
- Result is the output what doInBackground() returns.
Android AsyncTask ProgressBar Tutorial
Here is a skeleton code for you to understand Android AsyncTask better:
Caution: Android AsyncTask Limitations
However there are few things to note about Android AsyncTask vs IntentService that we have explained in our previous blog.
Android AsyncTasks will operate till the activity for which AsyncTask is working is active. As soon as activity is destroyed, Asynctask is considered as cache as can be killed.
Also too much use to AsyncTask can affect your App’s user experience.
Therefore its better to use IntentService if you want the task to be completed irrespective of activity current state.To know more about IntentService click here.