I was reading the Android "Application Fundamentals" page located at
http://developer.android.com/guide/topics/fundamentals.html#actlife
and found this interesting information in the "Multitasking" section:

"Note that when you write an activity, you can make it stop or
continue running when it is moved to the background (see onStop() in
Activity Lifecycle). For activities that download data from the
network, it's recommended to let them continue downloading so the user
can multi-task."

As far as I was aware I had to create a service if I wanted to have a
background activity such as a media player, internet download, or
processing of data in files on the SD card continue when the activity
was no longer in the foreground.

I went and reread the onStop information on the "Activity Lifecycle"
page, which was referred to.  But I didn't find any mention of how an
activity might continue to run in the background after onStop was
called.

The "Activity and Task Design Guidelines" page at
http://developer.android.com/guide/practices/ui_guidelines/activity_task_design.html
also mentions something similar when it says:

"In addition, not all activities have the behavior that they are
destroyed when BACK is pressed. When the user starts playing music in
the Music application and then presses BACK, the application overrides
the normal back behavior, preventing the player activity from being
destroyed, and continues playing music, even though its activity is no
longer visible — as a visual substitute, the Music application places
a notification in the status bar so the user still has an easy way to
get to the application to stop or control the music. Note that you can
write an activity to stop when its screen is no longer visible, or to
continue running in the background — the latter was chosen for the
music player."

Is the information on these pages accurate?  Is there some mechanism
that I have missed to keep an activity running while it is in the
background?

I have been re-reading all this documentation because I currently use
services to perform these types of background functions, and I have a
cooresponding activity screen from which the user started the
background processing and where the user can see the progress and
status of the background service activity.

When the service has finished I need the user to be able to return to
the activity where they started the background activity.  I currently
have the service use a notification with an intent to start the
activity, but that requires that the notification intent for the
activity have the FLAG_ACTIVITY_NEW_TASK flag and ends up creating
another instance of the activity rather than returning to the one that
is already waiting in the background.  This has the additional problem
that the new instance is on a new task and so the back button and/or
calling Activity.finish() does not return back to the activity the
user came from prior to starting the activity that initiated the
background processing.

If the user stays on my activity screen while the background service
processing is running everything works fine.  But if they press the
home key and do some other tasks, getting back to the correct activity
stack from the service notification icon is the area where I am not
understanding how to bring that existing activity stack back to the
foreground.

As far as I can tell the only way to prevent the service notification
intent from creating a new instance of the activity is to set the
activity launchMode to singleInstance.  However that requires the
activity to be the root of a task and so prevents it from being part
of an activity stack.  In my case this activity can be reached from
more than one activity, and I need the activity stack to function
correctly so it will return back to the right place.

Any help from those that better understand how to get back to an
activity and its activity stack from a service would be greatly
appreciated.

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