AsyncTask is deprecated in Android 11/R

Rahul Sharma
2 min readFeb 20, 2020

--

What is AsyncTask ?

AsyncTask is used to do short running operation on background thread, and publish result on UI Thread.

Why AsyncTask is Deprecated ?

  1. Some versions have parallel execution and some have sequential execution. Inconsistency between version.
  2. Need proper cleanup on completion or onError like. cancelling or disposing.
  3. execute() will be only be called from main Thread, if there is a requirement to execute AsyncTask from background, then we need to use callback in main thread and execute() (practically AsyncTask have to start from Main thread)

Solution ?

  1. Coroutines
  2. LiveData
  3. Thread
  4. Executor Service

// Below in example of Coroutines.

suspend fun fetchUser(): User {

return GlobalScope.async(Dispatchers.IO) {

// make network call

// return user

}.await() }

--

--

Rahul Sharma
Rahul Sharma

No responses yet