AsyncTasks can be used in a number of contexts, not only in Activities. And
where they are used in activities, there are many way of plumbing them to
the activity lifecycle. Here are three scenarios that I would generally use
AsyncTasks for (rather than services):

1) Dynamically rendering an image for the UI (here I would usually let the
task run to completion and then discard the bitmap if the Activity had been
destroyed).

2) Loading thumbnails from a web server (here I might explicitly cancel the
task when the activity is destroyed)

3) Logging a user into a server (here I typically keep the task running and
maintain a static reference to the 'current' activity instance)

My point here is that the range of possible ways of using AsyncTask is
broad, and the best way is heavily dependent on the specific case, so it
ceases to be something that can be adequately covered in the javadocs. It's
better suited to a book on Android development in my opinion.

Tom.

On 2 October 2010 23:39, Prakash Iyer <thei...@gmail.com> wrote:

> Each his own. Try writing a AsyncTask that say lives for more than 10s and
> in the final result say updates a text field. Now before the AsyncTask
> completes, just change the orientation. I'd bet most developers would assume
> the text field will get updated. Most developers would be wrong as the
> AsyncTask's onResult will be calling the original object's (the one that did
> the execute) text field and not the one that is currently displayed. Can you
> make sure this works? Yes! Should this be documented? I would think it is
> absolutely essential.
>
>
> On Sat, Oct 2, 2010 at 6:25 PM, Tom Gibara <tomgib...@gmail.com> wrote:
>
>> AsyncTask is excellently documented:
>>
>> http://developer.android.com/reference/android/os/AsyncTask.html
>>
>>  <http://developer.android.com/reference/android/os/AsyncTask.html>AsyncTask
>> has no interaction with the Activity lifecycle, except for that which you
>> introduce with your own code.
>>
>> It's for precisely this reason that AsyncTask and Service are mostly
>> orthogonal: An AsyncTask is helpful way of delegating work onto a thread
>> other than the main thread; a Service exposes a component lifecycle so that
>> a UI-free operation can be managed by the system.
>>
>> Tom.
>>
>> On 2 October 2010 22:20, Prakash Iyer <thei...@gmail.com> wrote:
>>
>>> I'm not sure I agree with your comment that it is the wrong question. In
>>> fact your explanation of how a majority of the cases a Service runs in the
>>> same process is precisely why most people will consider these as
>>> alternatives. I was trying to point out to the OP that if the OP ever wanted
>>> to create something that lived independent of the UI and/or was planned to
>>> be used by more than one activity independently developed/distributed, a
>>> service would be a better choice...
>>>
>>> IMHO the AsyncTask is notoriously poorly documented. It has some really
>>> strange interactions with the Activity lifecycle... As some one pointed out
>>> the IntentService is probably a better choice in many cases. Which again
>>> brings to question the orthogonality of AsyncTask & Service...
>>>
>>>
>>> On Sat, Oct 2, 2010 at 5:09 AM, Dianne Hackborn <hack...@android.com>wrote:
>>>
>>>> On Fri, Oct 1, 2010 at 2:07 PM, Prakash Iyer <thei...@gmail.com> wrote:
>>>>
>>>>> Service, OTOH, is designed to work in the background without requiring
>>>>> user interaction. Problem is that for your Activity to display the results
>>>>> it must somehow interact with your service. Ideally you will want to spawn
>>>>> the service off in a separate process and use either Intents or AIDL to
>>>>> communicate back progress to the Activity.
>>>>>
>>>>
>>>> The vast majority of the time, there is no need to run the service in a
>>>> separate process and thus deal with all of the additional complexity of 
>>>> AIDL
>>>> and such.
>>>>
>>>> As another poster mentioned, IntentService is very useful.
>>>>
>>>> Also "AsyncTask vs. Service" is the wrong question.  These are almost
>>>> totally orthogonal to each other -- you very often may use an AsyncTask as
>>>> part of implementing a service for example.
>>>>
>>>> And if your service is running in the same process as your activity, it
>>>> is very easy to just bind to it from the activity, call on to it to get its
>>>> current state, and supply a callback for when the state changes, just using
>>>> normal Java coding.
>>>>
>>>> --
>>>> 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<android-developers%2bunsubscr...@googlegroups.com>
>>>> 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 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
>>>
>>
>>  --
>> 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
>
>
>  --
> 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
>

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