WHY ASYNC TASK IS IMPORTANT IN ANDROID? Varshita.M
Asynctask c# is a very important aspect in the android application architecture.
Async task is not but the usage of UI in a proper and easy way. The background
operations can be performed easily and the results can be displaced accordingly
on that UI thread and it doesn’t require any manipulation for the threads and
handlers.
For the thread and the handler async task is the helper class for it and it
doesn’t compose to a generic framework of the thread.
Async task used should be applied only for small tasks or the short operations
that lasts only for few seconds.
Whereas if you want to keep all the threads running for a longer period of time
then it is best to use Java APIs.
THE 4 STEPS TO EXECUTE ASYNC TASK
1.onPreExecute() -
The first step in how to use asynctask in Android starts from here. Before the execution of the task, this is evoked on the UI thread. This step is important for setting up the tasks in the user interface to show all the progress that has been done.
2.doInBackground(Params...)-
The first step is followed by the second one which is principal
requirement in the latest asynctask execution.
This task comes up immediately after finishing the execution of the
onPreExecute.
Whenever a task takes much longer time to get executed then this step is
involved in the computation of the background work.
All the parameters that are present in an asynchronous task are there in
this step. And the result that is obtained from this execution will be
got back the user by this step after which it will go back to the
previous step.
And for the purpose of posting the progress and more in units, the other
tasks like publishProgress (Progress...) can be used.
And by using onProgressUpdate(Progress...) all the computing tasks of
the UI thread would be published.
3.onProgressUpdate(Progress...)-
the third step is the best asynctask parameter of all that are
present.
which gets invokes in the UI thread after getting the call from
publishProgress(Progress...).
While the time taken for the execution is undefined. This method is
normally used to show all type of progress that goes in the user
interface when the execution in the background still goes on.
For instance, we can say it as the to show the progress bar that is
being animated or the show logs that are in the text field.
4.onPostExecute(Result)-
the final step is one of the most chief step in having the best android
asynctask.
after the completion of all the background stuff it becomes active in
the UI thread.
And the results that the user get from doing all the background stuff is
given to this as a parameter.