AlanSTACK
AlanSTACK

Reputation: 6065

Is AsyncTask deprecated now with the existence of AsyncTaskLoader?

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.

Upvotes: 5

Views: 9127

Answers (3)

Paresh Dudhat
Paresh Dudhat

Reputation: 1246

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.

Upvotes: 6

hqt
hqt

Reputation: 30276

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

Upvotes: 6

BladeCoder
BladeCoder

Reputation: 12929

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.

Upvotes: 4

Related Questions