What are the methods in Async Task?
Async Task: is a way to achieve multi threading in android. It has some advantages over normal java threads.
1. You don't need to use any Thread keyword, every thing will be taken care by AsyncTask class internally.
It creates threads internally.
2. Since android follows single threaded UI model, where other threads can't touch UI components directly.
But from async task you can directly touch UI components from all the functions except from doInBackGround().
AsyncTask class will have 4 functions.
a. onPreExecute() - this is the first function to be called before calling doInBackGround(). This runs in UI thread. You can touch ui from this.
b. doInBackGround() - this runs in background thread created by AsyncTask class. Don't touch ui from this method.
Write background heavy logic in this function, as it runs in background thread.
c. onProgressUpdate() - this function is used to update UI while performing background functionality. to execute this call publishProgress()
this also runs in UI thread.
d. onPostExecute() - once doInBackGround() is finished this function will be called. If you want to update any UI after doInBackGround() use this
function. This also runs in main ui thread.
Async task use case: If you want to upload some images to facebook server and want to update UI (progress bar) while uploading images using progress bar, then you can use AsyncTask.
Back To Top