Notifications from a service, forced Intent.FLAG_ACTIVITY_NEW_TASK problem

8759 views
Skip to first unread message

Torgny

unread,
Jan 11, 2009, 8:33:45 PM1/11/09
to Android Developers
I am trying to create a notification in a service to notify the user
that the application's data has updated in the background. I want the
application to resume when they click the notification. When I click
the notification I get this message in DDMS:

01-11 16:16:10.574: WARN/ActivityManager(56): startActivity called
from non-Activity context; forcing Intent.FLAG_ACTIVITY_NEW_TASK for:
Intent { comp={com.example/com.example.view.ExampleActivity} }

This forces the activity to start even if it is already paused or
stopped, so it never resumes, it just creates a new instance of it,
overlapping everything, including service connections. If the
application is already running, another copy of it starts on top of
it. How do I make Android just resume the activity?

Thankful for any help!

Warm Regards,
Torgny

Torgny

unread,
Jan 11, 2009, 9:20:42 PM1/11/09
to Android Developers
I have searched through all the documentation available on services,
and could not find anything related to this, and none of the examples
shed any light on this. After some further research and meddling with
my code I found that this does the trick inside my service:

Intent intent = new Intent(this, ExampleActivity.class);
intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
intent, 0);

This will match the action and category set when launching the
application from the application list or desktop and therefore only
one instance is ever created.

Gothy

unread,
Jan 12, 2009, 3:59:48 AM1/12/09
to Android Developers
Same problem with my app, but mine is not a main activity to do your
hack proposed.
The main problem is that when this happens passed parameters from
notification intent are ignored and activity starts with initial
parameters, like it was opened again during usual navigation...
Those intents are making me angry again and again :E

Torgny

unread,
Jan 13, 2009, 12:48:33 PM1/13/09
to Android Developers
Is there any way that you can send extra data with the pending intent
to the activity?

A T

unread,
Jan 13, 2009, 12:50:20 PM1/13/09
to android-d...@googlegroups.com
try the singleTask launch mode in the activity tag of your manifest.

Dianne Hackborn

unread,
Jan 13, 2009, 1:06:37 PM1/13/09
to android-d...@googlegroups.com
Please be sure to read the Application Model doc, which outlines two of the most common ways to have notifications interact with applications.
--
Dianne Hackborn
Android framework engineer
hac...@android.com

Note: please don't send private questions to me, as I don't have time to provide private support.  All such questions should be posted on public forums, where I and others can see and answer them.

Torgny Bjers

unread,
Jan 13, 2009, 1:11:12 PM1/13/09
to android-d...@googlegroups.com
singleTask launch mode did the trick. No more duplicates running on top of each other. Thank you for the help!
--
Warm Regards,

Torgny Bjers
Phone: 336-793-6339
torgny...@gmail.com

Reply all
Reply to author
Forward
0 new messages