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.
Table of Contents
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.
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.
val executor = Executors.newSingleThreadExecutor()
val handler = Handler(Looper.getMainLooper())
* Your task will be executed here
* you can execute anything here that
* you cannot execute in UI thread
* for example a network operation
* This is a background thread and you cannot
* access view elements here
* its like doInBackground()
* You can perform any operation that
* requires UI Thread here.
* its like onPostExecute()
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
Hi, my name is Belal Khan and I am a Google Developers Expert (GDE) for Android. The passion of teaching made me create this blog. If you are an Android Developer, or you are learning about Android Development, then I can help you a lot with Simplified Coding.
15 thoughts on “Android AsyncTask is Deprecated: Here’s another way”
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.
The same thing, just use the Java syntax.
A right teacher or mentor would always be able to reply properly. What Ankush Mittal’s question but he didn’t get a proper reply. Why do you guys post any article like this when you don’t even know to reply properly….
Hi friend, I am not getting paid to do this thing. I do it voluntarily, so it is not possible to reply to everyone. Hope you understand. This is free content for everyone, not some paid course.
And anyone with java understanding can easily convert the kotlin code to java. Thank You
will you please make tutorial for java syntax
that means I have to use above code for room database
Yes, but if you are using coroutines, then no.
Will it make any performance issue? Because our app is large in size. Each and every screen we are using Asynctask. In onPostExecute we are parsing the response. Will the above thing will be helpful?
Its the same thing, with a different syntax. You can use it.
Awesome it works seamelessly, Finally, I don’t need to write more async tasks’s 🙂
For those who ask for java code refer to this StackOverflow answer https://stackoverflow.com/a/58767934/7793652
Looks like it does’nt work for java
Where to write the code for ProgressBar, while background operation is running.
How do you pass data from execute to post?
I am using AsyncTask
I wanted to convert i to the java.util.concurrent.Executors.
I have a: class weatherTask extends AsyncTask
and it includes:
protected void onPreExecute()
protected String doInBackground(String args) -> return string as a result
protected void onPostExecute(String result)
How i can convert it to the java.util.concurrent.Executors.?