5

To the best of my knowledge, AsyncTaskLoader not only has all the capabilities of AsyncTask but also incorporates best practices such as avoiding duplicate threads and premature death built-in.

Is there any justification for using AsyncTask anymore, or should I always use AsyncTaskLoader blindly? I ask this to determine if there is any exceptional scenario that I should be cautious about.

AlanSTACK
  • 5,525
  • 3
  • 40
  • 99
  • 1
    http://stackoverflow.com/questions/7120813/asynctaskloader-vs-asynctask – Daniel Nugent May 15 '17 at 23:13
  • 1
    "and additionally has in-built best practice features such as thread-duplication & pre-mature death prevention" -- `AsyncTaskLoader` has neither of those things, at least in terms of how I would use those terms. "Is there any reason to use AsyncTask anymore?" -- not everything fits the `Loader` pattern. "Or should I just blindly use AsyncTaskLoader everywhere" -- you should not "blindly" do *anything* related to threads. Different situations call for different thread usage patterns and different classes (e.g., `IntentService`, a custom `ThreadPoolExecutor`). – CommonsWare May 15 '17 at 23:37

3 Answers3

6
  • When you have a background job that need to be done no matter activity is destroyed or not: Service or IntentService of some mechanism of background job.
  • When you have a background job that need to be done and notify back again to UI: using AsyncTaskLoader.
  • When you have a background job that need to be done and notify back again to user and continue to run although activity is destroyed: using AsyncTask because AsyncTask continue to run when your activity is paused/destroyed/configuration changed ... In this case, be careful you will have memory leak/activity object is null. You must handle by yourself.

Every situation, there are different ways for handling and avoiding. But keep in mind above flow for easiest solution.

hqt
  • 29,632
  • 51
  • 171
  • 250
6

In 2017 when this question was asked, AsyncTask was still not deprecated. However, It is deprecated in Android 11 as AsyncTask requires a lot of checks to avoid memory leaks.

The commit in the Android ASOP project has the @deprecated notice:

@deprecated Use the standard java.util.concurrent or Kotlin concurrency utilities instead.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
Paresh Dudhat
  • 1,166
  • 1
  • 14
  • 28
4

AsyncTaskLoader is only useful to load data in an Activity or Fragment. AsyncTask is more versatile and can do any kind of background operation in any kind of component. There are alternatives like RxJava, HandlerThreads, simple threads, etc. but it's certainly not deprecated.

BladeCoder
  • 12,779
  • 3
  • 59
  • 51
  • Thanks for your response. Does that mean if I am trying to download some images/thumbnails and load some new content (comments/likes/etc) that AsyncTaskLoader > AsyncTask? Could you also give an example where AsyncTask is more versatile? – AlanSTACK May 16 '17 at 03:14
  • To load new content you would typically use AsyncTaskLoader. To load images it's preferred to use a specialized image loading library like Glide or Picasso. AsyncTask may be a good option to submit data once (like posting a form), be careful of lifecycle/memory leak issues though. You can also use AsyncTask to launch a one-shot async operation that may continue after the user leaves an activity, like writing data to disk or sending statistics. Or you can launch an AsyncTask from a Service if you need more control than IntentService. – BladeCoder May 16 '17 at 14:28