Why should you use AsyncTask?

676767676.jpg

Android supports multi-threaded programming which makes it possible to develop responsive applications. The thread that executes our application is called UI Thread or Main Thread. In UI Thread the commands are executed line by line, i.e next command line does not executes without finishing the current one. All of our application components (Activities, Services, ContentProviders, BroadcastReceivers) are created in this thread, and any system calls to those components are performed in this thread. But that does not means all the huge processes must be executed in the main or UI Thread.

We have background thread to handle such operation. Any potentially long task that may hang your application should be handled in a background thread. Typical examples of such tasks are network operations, which involve unpredictable delays. Users will tolerate some pauses, especially if you provide feedback that something is in progress, but a frozen application gives them no clue.

Why use background thread?

When we do not use background thread, all code of an Android application runs in the main thread and every statement is executed after each other. And If you perform a long lasting operation, for example accessing data from the Internet, the application blocks until the corresponding operation has finished. To provide a good user experience all potentially slow running operations in an Android application should run asynchronously in background thread. This includes all potential slow operations, like network, file and database access and complex calculations. It allows us to perform long/background operations and show its result on the UI thread without having to manipulate threads.

A better way to perform such background task is by using AsyncTask

AsyncTask

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.

The three types used by an asynchronous task are the following:

  1. Params, the type of the parameters sent to the task upon execution.
  2. Progress, the type of the progress units published during the background computation.
  3. Result, the type of the result of the background computation.

When an asynchronous task is executed, the task goes through 4 steps:

  • onPreExecute
  • doInBackground
  • onProgressUpdate
  • onPostExecute

onPreExecute : This step is used to setup the task

doInBackground : This step is used to perform the actual task that can take long time

onProgressUpdate : This step is used to update the current progress status of the task that is being perfromed in doInBackground.

onPostExecute : When doInBackground finishes executing the task, this step is used to deliver the result and stop the asynctask process.

 

 

Untitled-1.png

Example use of AsyncTask

class DoSomeTask extends AsyncTask<String, Integer, String>{

    @Override
    protected void onPreExecute() {
        //Setup precondition to execute some task
    }

    @Override
    protected String doInBackground(String... params) {
        //Do some task
        publishProgress (1);
        return "";
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        //Update the progress of current task
    }

    @Override
    protected void onPostExecute(String s) {
        //Show the result obtained from doInBackground
    }
}

nnmnmnmnm.jpg

Execute DoSomeTask

new DoSomeTask().execute("Some url");

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s