tags:

views:

79

answers:

2

Hey all

So, I'm both new to Java and to creating android apps, but not new to programming. I've read through most of the developer.android.com site, but I haven't been able to find this:

I want to make sure that a certain activity isn't running more than once at the same time. So we have a task somewhat like this:
Activity A) a TabActivity, which launches
Activity B) a ListView that, on-click, opens up
Activity C) which is the interface for a mediaplayer object

Right now, whenever somebody presses the back-button whilst in C (Which is a likely thing, because they're going to listen to a streaming 1-hour long mp3) and then presses another list item, instead of returning to C, C is opened a second time, and two streams are playing. Of course, I'd want only one instance of C running, and I want a second click on the list item to bring C back to the front. This could also be useful for notification intents.

I've been messing around with the flags (especially FLAG_ACTIVITY_NEW_TASK, FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_REORDER_TO_FRONT), but no success so far.

If someone could help me out here I could focus on my next challenge - making it a real feed reader :P

Thanks in advance,

A: 

You need to flag your actvity as either "singleTask" or "singleInstance" in your manifest. I can't remember the exact differences between the 2, but either should do what you want. SingleInstance just does something different with the stack.

You can handle new calls to startActivity() from the same activity instance with onNewIntent()

Falmarri
The details can be found here: http://developer.android.com/guide/topics/manifest/activity-element.html#lmode
elevine
I've tried that, in fact experimented with all possible combinations (for as far as I can see). Right now, both B and C have those android:launchMode properties attached and it didn't help.It can't be a MediaPlayer bug, for the regular MediaPlayer don't have that problem.
Vic V
Are you sure that there are 2 instances of activityC and you're not just starting the mediaplayer twice? Show us the code for activityc
Falmarri
http://pastebin.com/imx6y88s < I take it you're right about the mediaplayer running twice. But I have no clue on how to access it if I don't boot it up onCreate(). But I guess this is my in-experienced-ness kicking in. Thx
Vic V
have you tried defining `mp` as class member? stopping the player when the activity is destroyed and re-starting in `onCreate`?
Asahi
OK, that seems like the way to go. It looks like the activity lifecycle doesn't exactly go as I expected it to. Here's my new code: http://pastebin.com/pg1XgmTk.First time I press a list item, the activity opens and starts streaming. The second time, it mutes, but I still see the interface (I guess MP.release() kicks in). Third time, the app crashes (obviously there's no more mp left). It looks like onDestroy is ran when the second time an item is clicked?
Vic V
Try putting `new MediaPlayer()` in oncreate. Just declare mp as a class member. It's hard to know what's going on without posting how you're starting the activity and what you're doing in between.
Falmarri
The issue is that I can't make mp static, as soon as it's in the oncreate function. Eclipse will say 'Illegal modifier for parameter mp; only final is permitted'. I added a reset() before prepare() in onStart, and removed onDestroy(), and now it's real close. Everytime I click the list item, it restarts, whilst I actually want it in focus, unless a certain SharedPreferences has changed. I've learned a lot though. No more duplicate MP's running at this time!
Vic V
Why are you trying to make it static?
Falmarri
A: 

I've got it!

For those reading this question and wanting to know the summary: I mistakenly thought more then one activity was running, but it appeared more MediaPlayer instances where running. I made my mediaplayer a class member and am now controlling it from the onStart() event.

I am using SharedPreferences to check if the stream needs to reset and change source, or continue running and just show the interface.

Thanks for all your reactions. Really helped me out.

Vic V