The docs leave out a lot of details like that. That is why it is so
valuable to study good examples, such as the entry titled
"Multithreading for Performance" in the Android Developer's Blog at
http://android-developers.blogspot.com/search?updated-max=2010-08-04T15%3A00%3A00-07%3A00&max-results=7

Also, what you might have noticed form the online docs on AsyncTask
itself is that AsyncTask derives from Object, not from Activity. So it
is up to the client programmer (you) to establish the connection
between the 'Task' (your object deriving from AsyncTask) and the
Activity it (more precisely: its methods) is running in.

This in turn implies that your original question, "Does an AsyncTask
run every time the activity is brought back to the
front?" is out of place, since it is the Activity's callback,
onRestart() that gets called when the Activity is "brought back to the
front", if by 'front', you mean "visible to the user". But if by
'front' you mean "in the foreground", i.e. not only visible, but also
having input focus, then it is onResume() you are interested in (it
will get called after onRestart or onStart anyway). Neither has any
connection with what thread executes doInBackground() unless you, the
client programmer, establish the connection. Again, "Multithreading
for Performance" is a good example of how to do this. Normally, you
would call doInBackground from your Activity while in the running,
visible, foreground state, to create a new thread, which will pass
information back to the original main UI thread via publishProgress().
Then, when the system magically causes onProgressUpdate to be called,
this happens on the main UI thread, so that this where you can put
code to show the progress using UI widgets (e.g. updating a progress
bar).

Oh, about about back key and isFinishing(): of course, a user might
switch applications by press-hold on the Home key, too. In that case,
the Activity is NOT destroyed. It is only paused or stopped (if now
completely not visible).

Expect to have to read and re-read 
http://developer.android.com/guide/topics/fundamentals.html#actlife
many times to keep all these issues straight in your mind;)

On Feb 14, 11:33 am, Danielle Murkerson <[email protected]>
wrote:
> Ok that's good to know...I didn't see anything about that in the docs...So I
> could just tell it to cancel when the activity resumes.
>
> Thanks,
> DanielleM
>
> On Mon, Feb 14, 2011 at 2:32 PM, Kostya Vasilyev <[email protected]> wrote:
> > AsyncTask doesn't know anything about activities or their lifecycles.
>
> > If you leave your activity with the back key, it's actually destroyed. So
> > when you come back (by launching again, or by using the recent apps list
> > [long-pressing the home key]), it will be created all over again.
>
> > It's up to your code to handle activity lifecycle callbacks, and do what's
> > meaningful in this situation for your application (like canceling an
> > AsyncTask).
>
> > -- Kostya
>
> > 14.02.2011 22:15, DanielleM пишет:
>
> >  Hello all:
>
> >> I'm trying to use an AsyncTask to prepare two MediaPlayer objects
> >> while the rest of my activity loads. However, I've noticed that once
> >> everything is loaded...I'm able to play my media player just fine, but
> >> if I switch to another activity in my app and then switch back to the
> >> media player activity the Media Player objects get re-created and so
> >> the code I have in the onClick listeners for the buttons no longer
> >> works.
>
> >> Basically, the app loads, the streams are set up..once this is done
> >> you can press play to play one of the streams and then stop to pause
> >> it. But if I switch to another activity while the streams are playing
> >> and then go back to the MediaPlayer activity, I can no longer stop the
> >> streams.
>
> >> Does an AsyncTask run every time the activity is brought back to the
> >> front? That doesn't seem right since the code is in the onCreate
> >> method and the activity should not be re-created every time it's
> >> brought back to the front.
>
> >> If anyone can shed some light on what's happening here I'd really
> >> appreciate it.
>
> >> The buttons and streams were functioning properly before I started
> >> pushing the setup code to an AsyncTask...I just figured it was too
> >> taxing on the system to do the set up in the main thread. So I decided
> >> to push that to a background thread while the rest of the activity
> >> loads.
>
> >> Thanks,
> >> DanielleM
>
> > --
> > Kostya Vasilyev --http://kmansoft.wordpress.com
>
> > --
> > 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

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