AsyncTask is deprecated in Android 11/R
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 ?
- Some versions have parallel execution and some have sequential execution. Inconsistency between version.
- Need proper cleanup on completion or onError like. cancelling or disposing.
- 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 ?
- Coroutines
- LiveData
- Thread
- Executor Service
// Below in example of Coroutines.
suspend fun fetchUser(): User {
return GlobalScope.async(Dispatchers.IO) {
// make network call
// return user
}.await() }