Use of AsyncTask in Android

AsyncTask is an abstact class provided by Android which helps us to use the UI thread properly. This class around background operations and show the results on the UI without manipulate thread.

Usage:

Android implements input tasks with a single user interface thread and this thread is called Main Thread. Main Thread cannot handle concurrent operations because it can handle only one operation at a time.

If we are doing network operation on a button click our application. On button click a request would be made to the server and take a response. So we should avoid performing long running operations on the UI thread.

AsyncTask has four steps :

1. doInBackgound()

2. onPostExecute()

3. onPreExecute()

4. onProcessUpdate()

doInBackground() : When onClick method is executed on click of button, it calls execute method which accepts parameters and automatically calls in doInBackground().

onPostexecute() : This method is called after doInBackground() completes processing.

onPreExecute() : This method is called before doInBackground() is called.

onProgressUpdate() : This method is invoked by calling publishProgress anytime from this method call onProgressUpdate().