I made an app that makes streaming radio. How can I prevent that, the second time by clicking on the icon to open the app twice?
-
is your goal to allow an event the first time the app is launched, but prevent it the second time? – mfsiega Oct 24 '12 at 16:01
-
my goal is not to open the app if already open – Axiomatic.it Oct 24 '12 at 16:02
-
3I believe the android system will handle this for you. in other words, if your package is on the "currently running" list, it will bring the running instance to the foreground rather than launching it again. – mfsiega Oct 24 '12 at 16:03
-
it does not, because if you reopen the app the second time I hear the echo of two programs simultaneously. – Axiomatic.it Oct 24 '12 at 16:05
-
that seems the result of your implementation, not the app being opened a second time. without seeing your code, it's impossible to say why. I imagine that reviewing the activity lifecycle http://developer.android.com/reference/android/app/Activity.html would help sort it out for you – mfsiega Oct 24 '12 at 16:07
3 Answers
Here is one good suggestion:
* How to launch activity only once when app is opened for first time?
What I've generally done is add a check for a specific shared preference in the main Activity: if that shared preference is missing then launch the single-run Activity, otherwise continue with the main activity. When you launch the single run Activity create the shared preference so it gets skipped next time
This solution is slightly different than the question you're asking, but should give you exactly the result you're looking for.
Another possibility might be to use android:launchMode = "singleInstance"
in your androidmanifest.xml:
IMHO...
-
It seems that is streaming audio to open several times, and when I close the app closes everything, so your hypothesis seems plausible. – Axiomatic.it Oct 24 '12 at 16:13
To stream the music you should be using a Service so that you can keep it going even when your app is closed. At that point your Activity is only used to communicate with the Service. When you launch your app just check to see if the Service is running or not.

- 24,780
- 14
- 77
- 112
Typically this shouldn't happen, if your activity is already running the system will bring it it up if the user relaunches. If your activity is launched by another app/activity in a new task, or for some other reason an intent may be used to create a new instance of your already running activity, look into singleInstance or singleTask modes in the manifest:
http://developer.android.com/guide/topics/manifest/activity-element.html#lmode
As for streaming, you should use a Service (preferable a bound service) to contain your media player so that it can continue to play in the background, then the activity is just a client of the service that connects to it, and the service keeps a single instance. That has been described here:

- 1
- 1

- 1,518
- 13
- 12