I would like to know how it works. I have read the documentation a couple of times but don’t get the idea.
I noticed that in some cases onPostExecute does nothing.
Can anyone please give me a little explanation?
AsyncTask is a class derived from Thread and provides you a simple and proper way of doing some things in the background with the ability of notifying the UI Thread.
In order to use it you should create a class, which extends it and define the type parameters. They are Params, Progress and Result. Read about them more here.
onPostExecute() is a method which is called when doInBackground() finished it's execution, but onPostExecute() is run on the UI Thread. So, you can notify the UI about the work has been done.
You can see an example of AsyncTask usage here.
The <Void, Void, Long> part is what in Java is called Generics. It is used in those classes where the original coder wants it to be used no matter what the types the "end user" will choose.
Typically you use onPre/PostExecute() when you want to start/stop a progress dialog.
If you want to update the UI during the progress without a progress dialog you can do things in onProgressUpdate() (i.e.: you're loading images from web and displaying them as they get loaded)
If you have a dialog with progress bar you'll have to use all of them to update the progress bar.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With