AsyncTasks in Android
AsyncTasks in Android
AsyncTask is an abstract class in Android that offers us the freedom to execute demanding tasks
in the background while keeping the UI thread light and the application responsive. When
launched, an Android application operates in a single thread. Due to this single-thread approach,
tasks that take a long time to fetch a response may cause the program to become unresponsive.
We use Android AsyncTask to perform these heavy tasks in the background on a separate thread
and return the results back to the UI thread in order to prevent this. As a result, the UI thread is
always responsive when AsyncTask is used in an Android application.
The purpose of AsyncTask was to make it possible to use the UI thread correctly and
conveniently. The most frequent use case, however, was UI integration, which led to Context
leaks, missed callbacks, or crashes when settings changed. Additionally, it behaves differently
depending on the platform version, swallows exceptions from doInBackground, and offers little
benefit over using Executors directly. AsyncTask is not intended to be a general-purpose
threading system; rather, it is intended to be a helper class for Thread and Handler. AsyncTasks
are best used for brief operations (a few seconds at the most.) It is strongly advised that you use
the various APIs offered by the java.util.concurrent package, such as Executor,
ThreadPoolExecutor, and FutureTask, if you need to keep threads running for extended periods
of time.
Asynchronous tasks are divided into three generic types: Params, Progress, & Result and
four steps:
onPreExecute,
doInBackground,
onProgressUpdate, &
onPostExecute.
The following lists the three generic types utilized in an Android AsyncTask class:
doInBackground(): The code that has to be run in the background is contained in the
doInBackground() method.
The publishProgress() method in this method allows us to repeatedly deliver results to the UI
thread. We only need to use the return statements to signal that the background processing has
been finished.
onPreExecute(): The code that runs prior to the beginning of the background processing is
contained in this function.
onProgressUpdate(): This method can use the progress updates it receives from the
publishProgress method, which publishes progress updates from the doInBackground function,
to update the UI thread.
AsyncTask Example
The following code snippet must be present in the MainActivity class in order to launch an
AsyncTask.
myTask.execute();
The execute method is used to start the background thread in the excerpt above, where we’ve
used a sample class name that extends AsyncTask.
Java code:-
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
task.execute(10);
@Override
super.onPreExecute();
// ...
@Override
// ...
return "Finished!";
@Override
super.onProgressUpdate(values);
// ...
}
@Override
super.onPostExecute(string);
// ...
}
Kotlin code:-
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
myTask.execute(10)
class MyTask {
super.onPreExecute()
// ...
// ...
super.onProgressUpdate(values);
// ...
}
super.onPostExecute(result)
// ...
Reference :-
https://www.geeksforgeeks.org/asynctasks-in-android/