1

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).

Fahad Ishaque
  • 1,916
  • 1
  • 17
  • 21

5 Answers5

4

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();
    }
Tanmay Mandal
  • 39,873
  • 12
  • 51
  • 48
2

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.

Budius
  • 39,391
  • 16
  • 102
  • 144
  • I do like your suggestion, but the reason for AsyncTask is ThreadPoolExecutor, which means no need to take tension about their existence upon finish of task. What is your say? – Fahad Ishaque Apr 09 '13 at 13:50
  • 1
    if you're only executing non-UI thread, it means you're doing anything with the Views or Window. That way, even if the process still running after the activity dies, so what? Are you calling cancel() on the AsyncTask and checking for the isCancelled() on the doInBackground()? Even if you answer yes, you can still do the same with a thread. – Budius Apr 09 '13 at 13:56
  • 2
    So it makes no difference if the activity dies or not. – Budius Apr 09 '13 at 14:09
1

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

Mufrah
  • 534
  • 7
  • 22
1

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

klmprt
  • 651
  • 6
  • 7
0

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 .

dharmendra
  • 7,835
  • 5
  • 38
  • 71
  • @FahadIshaque Its strange behavioral of android for asyncthread most of time its close and sometimes it may depends on GC load , see that question, http://stackoverflow.com/questions/12117031/what-happens-to-an-asynctask-when-the-launching-activity-is-stopped-destroyed-wh – dharmendra Apr 09 '13 at 14:16
  • Yes you are right. If Main-Task loses its preference, its collected by GC. – Fahad Ishaque Apr 09 '13 at 15:21