Android - AsyncTask
About
The class AsyncTask simplify background thread creation.
It's then not the best pattern for a very long background operation. See Service
Articles Related
Disadvantage
Note that when you start an AsyncTask, it is tied to the activity you start it in. When the activity is destroyed (which happens whenever the phone is rotated), the AsyncTask you started will refer to the destroyed activity and not the newly created activity. This is one of the reasons why
using AsyncTask for a longer running task is dangerous.
Implementation
There are four important methods to override:
onPreExecute - This method is run on the UI before the task starts and is responsible for any setup that needs to be done.
doInBackground - This is the code for the actual task you want done off the main thread. It will be run on a background thread and not disrupt the UI.
onProgressUpdate - This is a method that is run on the UI thread and is meant for showing the progress of a task, such as animating a loading bar.
onPostExecute - This is a method that is run on the UI after the task is finished.