Reputation: 6065
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
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
Reputation: 30276
Service
or IntentService
of some mechanism of background job.AsyncTaskLoader
.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
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