On Fri, May 1, 2009 at 8:07 PM, Mark Murphy <[email protected]> wrote:

> It looks like you are trying to manually instantiate a Handler, or
> possibly an Activity, in the background thread. If so, that is not the
> proper way to use a Handler. This is one of the reasons I dislike
> Handler, preferring runOnUiThread() (and hopefully the new AsyncTask).
>
Yes, it seems that the proper way is to instantiate the Handler when being
on the UI thread.


> >   When asking the original initiator of the background operation to pass
> > in a handler, it might happen, that the activity that created the
> > handler is no longer active when the operation finished. I also believe
> > to remember that this will bring out an exception too.
>
> Doubtful. Otherwise, AsyncTask couldn't work.
>
Thanks for the heads-up. I've never seen AsyncTask before and meanwhile it
seems that it's an Android 1.5 feature. Shouldn't the @since attribute be
available in the Java doc output?
Anyway, AsyncTask instantiates its own
handler<http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=core/java/android/os/AsyncTask.java;h=ee4e8974b5c7c50ff97d20d3af6f27f25a725bb2;hb=HEAD#l152>
.

 >   All that looks very ugly, which often means that I don't get what the

> > proper way of doing things around here is.
> >
> >   But if the above mentioned way (passing in a handler from the original
> > activity) is the best way, then I will just do that.
>
> Can you use AsyncTask for this? The result of your background work would
>  be passed to onPostExecute(), which could then display an error on the
> UI thread if something failed in the background thread.
>
Yes, I could, in the future. Currently my app is sdk level 1 compatible
though and only 30% of my users are on sdk level 3.
(I just had a quick look, but it seems pretty self contained and I didn't
see any sdk level 3 api's used. So maybe I will copy it over for the time
being.)

But now I have confirmation that the "ugly" method seems to be the way to
go. I will pass in a handler, or do it like AsyncTask and expect the creator
of the background task to be on the UI thread and create the handler under
the covers.
The AsyncTask uses a static variable to hold the handler. I guess that means
that this just has to happen once and you can keep your handler until your
process dies.

I am still not sure about the handler lifecyle. I will have to test what
happens when my task ends when no part of my app's GUI is in the
foreground.

Mark, as always, you were very helpful. Thank you!

Cheers,
Mariano

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