Toasts must be shown from a thread that is running a handler, and by doing
so you are causing that thread to be a UI a thread for the duration of a
toast with the corresponding constraints (it will not execute its work if
you block it with your own stuff).

You are only living on luck if you touch anything in the view hierarchy from
another thread.  The view hierarchy is very, very single threaded.  There
are some sanity checks at places for calls coming in from the wrong thread,
but they won't catch everything, and the things they don't will result in
randomly broken behavior depending on how threads race or not.

On Mon, Jan 10, 2011 at 5:09 AM, Shawn Brown <big.coffee.lo...@gmail.com>wrote:

> Hi,
>
> Android docs(1) suggest using threads and a handler to send messages
> (to avoid ANR).
>
> Fine.  I've always done that but then porting some code to Android
> noticed that it's not necessary.
>
> Passing the Activity (i.e. Context) works fine.
>
> public class WorkerThread extends Thread {
>
>    private Activity mActivity;
>
>    public WorkerThread(Activity mActivity) {
>        super();
>        this.mActivity = mActivity;
>    }
>
>  @Override
>    public void run() {
>      mActivity.showToast("WorkerThread thread says hello");  //
> displays a message
>      //setting a TextView works fine too
> ...
> }
>
> I understand the potential for memory leaks (2), but if I control the
> WorkerThread lifecycle (it dies when the the Activity  pauses), then
> really there is no need to use a handler, is there?  Or do I
> misunderstand?
>
> Shawn
>
> (1) Handling Expensive Operations in the UI Thread -
> http://developer.android.com/guide/appendix/faq/commontasks.html
> (2)
> http://android-developers.blogspot.com/2009/01/avoiding-memory-leaks.html
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscr...@googlegroups.com<android-developers%2bunsubscr...@googlegroups.com>
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
>



-- 
Dianne Hackborn
Android framework engineer
hack...@android.com

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 android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to