Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is AsyncTask deprecated now w/ AsyncTaskLoader?

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

like image 349
AlanSTACK Avatar asked May 15 '17 23:05

AlanSTACK


People also ask

What is the replacement of AsyncTask?

AsyncTask is used to perform time talking operations in android, but it's marked as deprecated from android 11. There are many alternatives are available for replacing an AsyncTask, one of the replacements is ExecutorService.

Can I still use AsyncTask?

AsyncTask Alternatives Since AsyncTask is deprecated now, it leaves a bit of a void that must be filled by some other concurrency approach. So, let's discuss AsyncTask alternatives. If you just start your Android journey and use Java, I recommend the combo of bare Thread class and UI Handler.

Can I still use AsyncTask Android?

According to the Android documentation AsyncTask was deprecated in API level 30 and it is suggested to use the standard java. util. concurrent or Kotlin concurrency utilities instead.

What can I use instead of AsyncTask in Java?

The officially recommended alternative is Kotlin Coroutines, that you must use of writing Asynchronous Code in your project.


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

like image 57
hqt Avatar answered Sep 21 '22 19:09

hqt


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.

like image 42
Paresh Dudhat Avatar answered Sep 24 '22 19:09

Paresh Dudhat