Why does AsyncTask run in the main Thread of an application?

前端 未结 2 1650
庸人自扰
庸人自扰 2020-12-15 07:48

in my application, I have a class for UI stuff, the name of which is \"SettingActivity\".

Then for doing some jobs in background, I bind this UI class(SettingActivi

相关标签:
2条回答
  • 2020-12-15 08:29

    In fact, I have checked the doc of android, onPreExecute(), onProgressUpdate(Progress...) and onPostExecute(Result) are all "invoked on the UI thread".

    Only the doInBackground(Params...) method is "invoked on the background thread".

    0 讨论(0)
  • 2020-12-15 08:33

    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.

    0 讨论(0)
提交回复
热议问题