Is AsyncTask deprecated now w/ AsyncTaskLoader?

Viewed 8858

Since AsyncTaskLoader does everything AsyncTask can do, and additionally has in-built best practice features such as thread-duplication & pre-mature death prevention.

Is there any reason to use AsyncTask anymore? Or should I just blindly use AsyncTaskLoader everywhere

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

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.

Related