Get In Touch
Ahmedabad, India
[email protected]
+91 9925757082
Get In Touch
Santa Cruz de la Palma, Spain
[email protected]
+1 (917) 668-2504

AsyncTask is Deprecated : What are the alternatives to it?

What is AsyncTask ?

AsyncTask is an abstract class. It is an asynchronous task that runs on the background thread and executes results on the UI thread.

Currently, AsyncTask is deprecated in API level 30 because of its inconsistent behavior on different versions and swallows exceptions from the doInBackground method. It also causes Context leaks, missed callbacks or crashes on configuration changes.

AsyncTask is a helper class around Thread and Handler. It should be used for short operations. If you want to run threads for a long period of time you can use various APIs provided by java.util.concurrent like ExecutorThreadPoolExecutorFutureTask.

Now what is an alternative for deprecated AsyncTask?

In the official document, it recommended Kotlin concurrency utilities and java.util.concurrent as an alternative.

In this article, We will use two methods. First is Using Executor and Handler & Second is Using Thread.  

1. Using Executor and Handler

Executor and Handler can be found in the java.util.concurrent package. The Executor performs background tasks while the Handler changes the user interface.

Here is an example of it.

Create instance of Executor

private val executor: Executor = Executors.newSingleThreadExecutor()

When the Executor executes a task and gives a result we will use Handler for UI changes. getMainLooper() method returns the main looper, which lives in the main thread.

private val handler = Handler(Looper.getMainLooper())

Execute method used to perform tasks asynchronously.

executor.execute {
// perform task asynchronously
handler.post {
// update the result to the UI thread
}
}

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import androidx.appcompat.app.AppCompatActivity
import java.util.concurrent.Executor
import java.util.concurrent.Executors

class MainActivity : AppCompatActivity() {

   private val executor: Executor = Executors.newSingleThreadExecutor()
   private val handler = Handler(Looper.getMainLooper())

   override fun onCreate(savedInstanceState: Bundle?) {
       super.onCreate(savedInstanceState)
       setContentView(R.layout.activity_main)

       executor.execute {
           // perform task asynchronously
           handler.post {
               // update the result to the UI thread
           }
       }

   }
}

2. Using Thread

A thread refers to a single sequential flow of control within a program. It is one of the easiest alternatives. We can simply say that a thread is a way to execute a program.

Here is an example of how we can use the Thread:

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

   override fun onCreate(savedInstanceState: Bundle?) {
       super.onCreate(savedInstanceState)
       setContentView(R.layout.activity_main)

       Thread {
           // Do something in Background
           runOnUiThread {
               // Do something in UI
           }
       }.start()
   }
}

Now, Let’s review the runOnUiThread:

In some cases, the Main thread performs heavy operations. The user can add extra operations on the UI by loading the ANR and providing it. By using runOnUiThread, you will be able to perform the background operations on the worker thread while updating the result on the main thread.

Conclusion

As a result, we have learned an alternative to the deprecated AsyncTask.

Author avatar
Anjana Gausvami