Android AsyncTask is Deprecated: Here’s another way

If you are into android development then I am pretty sure that you know about Android AsyncTask. The AsyncTask class helped us in executing some code in the background thread.

With the help of AsyncTask we can execute something on the background thread and get the result back in the UI thread. If you want to learn about AsyncTask then you can check this Android MySQL Tutorial, where I used AsyncTask.

But Android AsyncTask is deprecated in API Level 30.

So what are the alternative now? That is what I will tell you in this post.

Why Android AsyncTask is Deprecated?

Here is the official reason it is deprecated.

AsyncTask was intended to enable proper and easy use of the UI thread. However, the most common use case was for integrating into UI, and that would cause Context leaks, missed callbacks, or crashes on configuration changes. It also has inconsistent behavior on different versions of the platform, swallows exceptions from doInBackground, and does not provide much utility over using Executors directly.

Alternative of AsyncTask

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

You can check this complete Kotlin Coroutines Tutorial that I have already published.

But if you are a beginner and you just started learning android development then jumping directly into Kotlin Coroutine is not recommended. So in this post I will show you something that do not requires any dependency.

Using Executors

We have java.util.concurrent.Executors class; that we can use in place of AsyncTask if you do not wish to use Kotlin Coroutines.

Here is an example how you can use it.

It is pretty much the same thing that you do using AsyncTask .

But still it is recommended that you use Kotlin Coroutines for writing asynchronous codes in your project.

So that’s it for the alternative of Android AsyncTask. If you have some inputs from your side please comment it below. And yes don’t hesitate in asking if you have any confusion or problelm. Thank You

2 thoughts on “Android AsyncTask is Deprecated: Here’s another way”

  1. I make a application using java language, Kindly guide me what can i use in place of AsyncTask in java. To call server api calls.


Leave a Comment