Fahad Ishaque
Fahad Ishaque

Reputation: 1926

AsyncTask for longer Processes

I know that AsyncTask are not preferred for long process. Their main objective is to take the load off from UI thread and do stuff in background. Later on completion update the respective UI thread.

I am aware of the memory leaks i.e when UI needs to be updated after doInBackground is done and there's a possibility that the activity is destroyed.

My question is can I use AsyncTask just like a simple Thread?

What happens to AsyncTask if Activity or Application dies, which started it?

Requirments

I dont need to update any UI.

I dont want my task to be associated with Activity(which starts it).

Upvotes: 1

Views: 501

Answers (5)

dharmendra
dharmendra

Reputation: 7881

My question is can I use AsyncTask just like a simple Thread?

Yes AsyncTask is android background thread , the task will be done in the background . AsyncTask automatically creates a new Thread for you, so everything you do in doInBackground() is on another thread.

What happens to AsyncTask if Activity or Application dies, which started it?

The AsyncTask is related to application , if application destroy or finish then all related AsyncTask of that application will be terminated .

Upvotes: 0

klmprt
klmprt

Reputation: 661

First, a general note, as stated by the Android Docs:

AsyncTasks should ideally be used for short operations (a few seconds at the most). If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs provided by the java.util.concurrent pacakge such as Executor, ThreadPoolExecutor and FutureTask.

To answer your questions:

  1. Yes - you can use Async task as if it were just a background thread - an Async task is merely a wrapper of Thread and Handler that allows the thread to seamlessly communicate with the UI thread. Warning! If you plan to update the UI thread, or otherwise reference an activity or fragment in the callbacks that reference the UI thread (i.e. onProgressUpdated and/or onPostExecute) you should explicitly check that the activity or fragment is still in a state from which it can be referenced and used. For example - here's the right and wrong way to do it when launching an AsyncTask from a fragment:

Create your task with a ref to the activity so you can do something when it's done:

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
    Fragment mFragment;

    public DownloadFilesTask(Fragment fragment){
        mFragment = fragment;
    }

WRONG:

    protected void onPostExecute(Long result) {
        // if the fragment has been detached, this will crash
        mFragment.getView().findView...
    }

RIGHT:

    protected void onPostExecute(Long result) {
        if (mFragment !=null && mFragment.isResumed())
            ... do something on the UI thread ...
    }
}
  1. If the Activity dies while an AsyncTask is executed, it will continue to run. Using the techniques listed above, you can avoid crashes by checking the lifecycle of the context that started the task.

  2. Finally, if you have a very long-running operation that does not require the UI thread at all, you should look into using a Service. Here's a blurb:

A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use

Upvotes: 1

Tanmay Mandal
Tanmay Mandal

Reputation: 40168

First Question :

Yes you can.Its totally depends on your logic.

Second Question :

The thread will be in the background though the application is killed by the user or by the system.

To resolve the second scenario use the following technique

Just make sure that you are finishing your AsyncTask before application or Activity closes

AsyncTask yourAsyncTask

    @Override
    public void onDestroy(){
        //you may call the cancel() method but if it is not handled in doInBackground() method
        if(yourAsyncTask!=null)
        if (yourAsyncTask != null && yourAsyncTask.getStatus() != AsyncTask.Status.FINISHED)
            yourAsyncTask.cancel(true);
        super.onDestroy();
    }

Upvotes: 4

Budius
Budius

Reputation: 39836

If you only need the 'doInBackground' just use a normal thread.

new Thread("threadName", new Runnable(){ @Override run(){ } }).start();

The whole reason to use an AsyncTask is to have the facilities of preExecute and postExecute, so you don't need to mess with handlers.

Upvotes: 2

Mufrah
Mufrah

Reputation: 534

It remain started in background even the application is killed or crash.

Upvotes: 1

Related Questions