In this tutorial we will see how to use AsyncTask in your android application . AsyncTask is an abstract class provided by android ,which helps in performing background operations and updating the results on the UI Thread.
AsyncTask has 3 generic Types :
Param : The parameter sent to the task upon execution
Progress : The parameter sent during background computation to show progress unit
Result : This Parameter presents result return by background computation
AsyncTask has 4 Steps :
onPreExecute( ) : Invoke the UI Thread after the task is executed, This method is normally is used to setup the task .
doInBackground(Param…) : Invoked on background thread immediately after the onPreExecute() finishes executing, This is generally used to perform background operation which takes long time.
onProgressUpdate(Progress…) : Invoked on the UI thread after a call to publishProgress(Progress…) inside doInBackground() ,This method is generally used to show the progress of the task .
onPostExecute(Result…) : Invoked on the UI thread after the background computation finishes. this method takes the parameter result which is returned by doInBackground() method .This method is generally the end of task , and where you can update the result
Using AsyncTask :
To Use AsyncTask , your class must subclass to it and must override at least one method doInBackground() and most often override one more method called onPostExecute to update the result, In this we will be focusing on using all the 4 methods of AsyncTask Lets see an Example of AsyncTask