You can also spawn an AsyncTask from the UI thread. Whenever it needs to show a dialog, this task can call publishProgress() method..that automatically runs on the UI Thread and the dialog can be shown in the onProgressUpdate method.
On Fri, Jan 22, 2010 at 3:41 AM, Kevin Duffey <[email protected]> wrote: > Ah yes Streets.. that was what I was missing.. been a while since I dealt > with UI threads. It's not that you have to run a thread on the UI.. you run > a Runnable. The Runnable just defines run() method.. I forget now but I > think basically it allows the UI thread to just call run() on your Runnable > you pass in to run your UI code.. but it allows you to access the UI thread > from outside of that thread (in another thread) so that any UI stuff you > need to access or do is possible.. since it will now run on the UI thread. > The reason you need to do this is that in a separate thread, and code you > use that expects to use the UI in some way, will not be able to access the > UI from a different thread. There is quite a bit of code that uses something > like Thread.getContextLoader().... and if that call is made while in a > separate thread, it will fail if it's expecting to do some UI stuff. When > you put that Runnable wraper of your UI code on the UI thread, the UI is > available. > > > > On Thu, Jan 21, 2010 at 2:01 PM, Streets Of Boston < > [email protected]> wrote: > >> Note that 'post' and 'runOnUiThread' takes a Runnable and it should >> *not* be a Thread (although that implements Runnable as well)! (Mis) >> using a Thread for this is not good. Just use a Runnable. >> >> Create a new instance of a Runnable and implement its 'public void run >> ()' method (see my example code in my previous post). >> >> On Jan 21, 4:50 pm, TreKing <[email protected]> wrote: >> > On Thu, Jan 21, 2010 at 3:30 PM, Dirk Vranckaert >> > <[email protected]>wrote: >> > >> > > It seems your solution does not work either. >> > >> > Um ... it should, I've used this technique repeatedly. Can you post the >> code >> > you're using? >> > >> > > As long as I'm doing it in a thread it doesn't work. >> > >> > Well, it depends on how you're running the thread. If you're posting it >> to >> > the runOnUiThread() function, it should work. If you're starting it with >> > Thread.start(), then yeah, that won't work. Again, some code would >> clarify. >> > >> > > Altough what I tried next was setting the error message for the user >> > > in my catch clause. And right after the thread finished I handled the >> > > showing of the errors based on wheater some error message was set or >> > > not! >> > >> > I'm not sure I follow here. >> > >> > > Thx for your help! (If you get me some advanced explanation on why >> > > dialogs cannot be created from within a thread plz let me know!) >> > >> > Probably because of the complexities of ensuring thread safety. If you >> could >> > update the UI from any thread, you could run into various threading >> issues >> > (deadlocks, race conditions, etc). If they core framework were designed >> to >> > support this it would introduce more complexity and overhead to the >> system. >> > Just a guess, but again, this is pretty standard behavior in GUI >> systems, so >> > it's really something that's just accepted. >> > >> > >> ------------------------------------------------------------------------------------------------- >> > TreKing - Chicago transit tracking app for Android-powered >> deviceshttp://sites.google.com/site/rezmobileapps/treking >> >> -- >> You received this message because you are subscribed to the Google >> Groups "Android Developers" group. >> To post to this group, send email to [email protected] >> To unsubscribe from this group, send email to >> [email protected]<android-developers%[email protected]> >> For more options, visit this group at >> http://groups.google.com/group/android-developers?hl=en >> > > -- > You received this message because you are subscribed to the Google > Groups "Android Developers" group. > To post to this group, send email to [email protected] > To unsubscribe from this group, send email to > [email protected]<android-developers%[email protected]> > For more options, visit this group at > http://groups.google.com/group/android-developers?hl=en > -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

