execute(Params…) method that executes it, must be invoked on the UI thread.Ĥ. The task instance must be created on the UI thread.ģ. This is done automatically as from JELLY_BEAN.Ģ. This class must be loaded on the UI thread. There are a few threading rules that must be followed for this class to work properly:ġ.
The result of the background operation is passed to this step as a parameter and then we can easily update our UI to show the results. onPostExecute(Result) – This method is invoked on the main UI thread after the background operation finishes in the doInBackground method. We can also update our progress status for good user experience.Ĥ. This method is used to display any form of progress in the user interface while the background operations are executing. onProgressUpdate(Progress…) – This method is invoked on the main UI thread after a call to publishProgress(Progress…). These values will be published on the main UI thread in the onProgressUpdate(Progress…) method.ģ. This method can also use publishProgress(Progress…) to publish one or more units of progress. The result of the operations must be returned by this step and it will be passed back to the last step/method i.e onPostExecutes(). The parameters of the Asynchronous task are passed to this step for execution. Main purpose of this method is to perform the background operations that can take a long time. doInBackground(Params) – This method is invoked on the background thread immediately after onPreExecute() finishes its execution. This method is mainly used to setup the task for instance by showing a ProgressBar or ProgressDialog in the UI(user interface).Ģ. onPreExecute() – It invoked on the main UI thread before the task is executed. Here are these four methods of AsyncTasks.ġ. In Android, AsyncTask is executed and goes through four different steps or method.
ResultValue: ResultValue is the type of the result of the background computation. ProgressValue: Progress is the type of the progress units published during the background computation.ģ. TypeOfVarArgParams: Params is the type of the parameters sent to the task upon execution.Ģ. The three types used by Asynchronous task are the following:ġ. New DownloadFilesTask().execute(url1, url2, url3) AsyncTask’s generic types In Android: Here is the Syntax for execution of AsyncTasks classs. Syntax of AsyncTask class: private class DownloadFilesTask extends AsyncTask Executions of AsyncTask class from main thread: The parameters are the following AsyncTask. This method is called automatically after the doInBackground method finished its work. The onPostExecute() method of this class synchronizes itself again with the main UI thread and allows it to make some updating. This class will executes everything in doInBackground() method inside of other thread which doesn’t have access to the GUI where all the views are present. For providing a good user experience in our application we need to use AsyncTasks class that runs in a separate thread. If we need to perform long tasks/operations then our main thread is blocked until the corresponding operation has finished.