0

When a pending intent is sent, does it create a new activity? What if I have an activity already running? Is there a way to specify an already running activity, and have a method in that activity run once I send the intent?

What I want to do is have a button in a notification bar that acts a "stop" button, which will call the stop method in the already running application.

cbrad
  • 163
  • 1
  • 15
  • I noticed in a comment that you are trying to play media using activities. This won't work. Android will kill your process (and the activities) relatively quickly after it goes to the background (on most phones). For playing media in the background you will need to implement a `Service`. – David Wasser May 16 '14 at 13:21

1 Answers1

1

It depends on the Activity's declaration in the manifest, or the Intent flags that you include.

For example, if you use FLAG_ACTIVITY_SINGLE_TOP (or the activity has launchMode set to "singleTop") then onNewIntent() will be called on the existing activity instead of creating a new one.

In your example, you should pass an extra in the intent to indicate that you want to execute the "stop" action, then check for it in onNewIntent().

This is well explained in the official documentation about launch modes: http://developer.android.com/guide/components/tasks-and-back-stack.html#TaskLaunchModes

EDIT: However, since the ultimate objective was playing audio in background, using a Service is a more appropriate option. Check http://developer.android.com/guide/topics/media/mediaplayer.html#mpandservices

To control the service from the notification (i.e. play, pause, stop) you need to supply PendingIntents created from with startService().

matiash
  • 54,791
  • 16
  • 125
  • 154
  • Thanks for your answer, going to try implementing it. With an app executing as a singleTop app, does it pause when I navigate back to my home screen for example? Or will it continue running in the background? Basically I have a media player stream I want to continue running in the background. – cbrad May 15 '14 at 22:36
  • @cbrad Yes, it will pause like any other activity. If you want to play audio in the background, you should use a service. See http://developer.android.com/guide/topics/media/mediaplayer.html#mpandservices – matiash May 15 '14 at 22:40
  • Sorry for the late reply, I believe a service is the way to go. Do you know anything about have a button within a notification that stops a service? – cbrad May 23 '14 at 02:14
  • Yes, you must use PendingIntent.startService() and associate these PendingIntents to the notification buttons. It's a little long to explain here, it would suggest taking a look at http://stackoverflow.com/questions/10516722/android-notification-with-buttons-on-it/10959343#10959343 (changing startActivity() for startService() &c). – matiash May 23 '14 at 02:25
  • Alright so you've pointed me in the right direction in terms of starting a service. What's the protocol of accepting your comment as an answer? – cbrad Jun 06 '14 at 21:52