You probably want to pass the view in (or just the handler).  If your thread
is updating some part of the UI, it would make sense to hand it the view it
is associated with.

As far as race conditions...  at the end of the day, there is some thread
that is going to draw that view hierarchy, and if you are not in that
thread, you are going to be racing with it.  Enqueueing a message in its
handler allows you to avoid those races, by propagating your state to it in
a controlled way: you update the state in your thread, deliver it on a
handler, and the state gets set in another thread.  If the state changes
before the other thread gets a chance to set it, well you will be sending
another message/runnable anyway with the new state that is guaranteed to
execute after whatever previous one you set.

That is the pattern, but you can make it more complicated.  For example if
you have a lot of state you can protect it with a lock.  Every time you
access it, acquire the lock.  When you modify it, lock, modify, send a
message to the view just that it has changed.  When the view gets the
message, it acquires the lock and does whatever is appropriate with the lock
held.  It will always setting down to having the most recent state.

On Thu, Feb 11, 2010 at 11:49 AM, Berek <[email protected]> wrote:

> How do you get a View from the current window (Activity?) using this
> method in a worker thread (not GUI)? And, even if you did, there is
> still a window (of execution) between the time you get the view and
> the time the runnable runs. The Activity may have changed in that
> window of execution.
>
> On Feb 11, 12:49 pm, Dianne Hackborn <[email protected]> wrote:
> > On Tue, Feb 9, 2010 at 9:39 AM, Frank Weiss <[email protected]> wrote:
> > > The conundrum on Android is that user code can't block in a UI thread
> and
> > > non-UI threads can't manipulate the UI.
> >
> > It is almost trivial to, from another thread, schedule a Runnable on the
> > window's thread to do work on it:
> >
> > void myThreadDoingStuff() {
> >     // ...
> >     final int viewVisibility = somethingWeComputed;
> >     //
> >     final View view = getSomeViewInWindow();
> >     view.getHandler().post(new Runnable() {
> >         public void run() {
> >             view.setVisibility(viewVisibility);
> >         }
> >     });
> >     // ...
> >
> > }
> >
> > --
> > Dianne Hackborn
> > Android framework engineer
> > [email protected]
> >
> > Note: please don't send private questions to me, as I don't have time to
> > provide private support, and so won't reply to such e-mails.  All such
> > questions should be posted on public forums, where I and others can see
> and
> > answer them.
>
> --
> 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
>



-- 
Dianne Hackborn
Android framework engineer
[email protected]

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.

-- 
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

Reply via email to