Why Does Asynctask Run In The Main Thread Of An Application?
Solution 1:
An AsyncTask has several parts that you can override: a doInBackground
method that does, in fact, run on a separate thread, and three methods—onPreExecute
, onProgressUpdate
, and onPostExecute
—that run on the UI thread. (The default implementation of these methods do nothing and onProgressUpdate
only runs if you call publishProgress
, usually from within doInBackground
.) The purpose of onPostExecute
is to publish results (such as updating the view hierarchy, or setting text in a text view) that must be done on the UI thread. It also can post progress updates. In order for this to all work properly, the AsyncTask must be created, and the execute
method called, on the UI thread.
You must not call UI actions from within doInBackground
-- doing so will crash your application.
Post a Comment for "Why Does Asynctask Run In The Main Thread Of An Application?"