I was using AsyncTask and just switched over to using a traditional Thread for reasons not worth going into for the sake of this question.
What can I do to make something execute before and after the thread starts, similar to the functionality of onPre/PostExecute?
For example, let’s say I want to display a loading circle before the thread starts, and dismiss it when it finishes. How would I do that?
For the post-execution bit, I’m using thread.join() and it seems to do the trick. I’m not sure if that’s the way to go about it though, or how to do something pre-execution.
onPreExecute()-> statements you execute before callingstart()on yourThreadonPostExecute()-> callrunOnUiThread(), orpost()on aView, from yourThread, supplying aRunnableto be executed on the main application threadIdeally, you wouldn’t. Progress dialogs suck. Put a progress indicator in your UI somewhere (e.g., action bar, title bar), and disable the few things the user cannot safely do while your thread is in operation.
That being said, you’d presumably display the
DialogFragmentbefore executing the thread, then remove the fragment once the thread is completed via the above mechanisms.Ick. That will block whatever thread you are on, and if that’s the main application thread, that’s really bad.
UPDATE
Example of
runOnUiThread():