Friday, September 7, 2012

Re: [android-developers] Thread

Streets has it right:  You probably want an AsyncTask.  Using an AsyncTask has several important advantages:  it uses a thread pool instead of creating and destroying threads and it also imposes order on things: tasks are executed in the order in which they are started.

If you actually need a long running process, look into Loopers.

I'm pretty suspicious of the whole WeakReference-to-the-Activity trick.  It seems to me that, as the architect of your application, you should design the lifecycles of your objects.  If your AsyncTask isn't doing anything you care about -- eg, your Activity is paused and might be GCed and you are willing to get the results again -- then you should cancel it.  If you want the results, you should attach the task to something long lived and arrange to get the value that it produces.

Leaving the decision to throw the results away, to the OS doesn't seem right.


G. Blake Meike
Marakana

The second edition of Programming Android is now on-line:
http://shop.oreilly.com/product/0636920023005.do

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

No comments:

Post a Comment