0

I simply want that my activity has only one instance. I read about intent flags and launchmodes, but it just refuses to work. I tried SingleTask, SingleTop, various intent flags etc. etc.
My manifest:

    <activity
        android:name="com.secret.domain.Player"
        android:label="@string/title_activity_player" 
        android:launchMode="singleTask">
    </activity>

and the launch code:

Intent intent = new Intent(getActivity(),Player.class);
intent.putExtra(Intent.EXTRA_TEXT,s);
intent.setData( Uri.parse( s ));
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

The Player activity obviously plays music, and still does it if I go back and create a new instance, both can play at the same time. OnNewIntent() is never called by the way.
I assume I've did something wrong, but I cant find out what.

EDIT: I know it sounds similar to other threads, but I read them and still couldn't figure out how to achieve what I want.

  • Also check this answer :http://stackoverflow.com/questions/6706645/single-instance-of-activity – moujib Mar 26 '13 at 10:19
  • I don't think that the problem in the Player `Activity`. Do you handle `onNewIntent()` there and what component do you use to play your music? – Alex Bonel Mar 26 '13 at 10:37
  • I use MediaPlayer for playing (in Player), onNewIntent() is never called at all - so I don't handle anything there. – AdmiralSnackbar Mar 26 '13 at 13:21

1 Answers1

0

So you have made a shot to your leg, he he )))

As your activity should be run in single task (because of singleTask attribute). Every time when you start it, the system creates a new task for your Activity retaining the previous instance and it's media player component in memory.

That's why you hear several music tracks from several created tasks.

In such situation you may either make Activity to be as a singleInstance (using appropriate attribute in manifest), then if you already have created task with Activity in it, you just will need to handle intent in onNewIntent() and you should start it without any Intent flags. But you should make this Activity to have ACTION_MAIN and CATEGORY_LAUNCHER filters as described here in the second paragraph. Or you should make it standard in your manifest and launch it with FLAG_ACTIVITY_CLEAR_TOP considering that you will lose all the top activities.

Alex Bonel
  • 1,344
  • 1
  • 9
  • 22
  • Nope, doesn't work either. By the way, I can't do the filters as described, it's not a stand alone activity (and it doesn't work as desired) – AdmiralSnackbar Mar 26 '13 at 13:34
  • So you want to say, that if you specify your activity in the manifest as `standard` and start it with `FLAG_ACTIVITY_CLEAR_TOP` intent flag, you don't get the result that you want? – Alex Bonel Mar 26 '13 at 13:47
  • Yep, I still get multiple instances. – AdmiralSnackbar Mar 26 '13 at 14:03
  • Then override `onDestroy()` and turn off the music player there. If I understand documentation correctly, with `standard` launch mode, the `Activity` will be also removed from stack when start the new one, as described [here](http://developer.android.com/guide/components/tasks-and-back-stack.html#TaskLaunchModes) Note: If the launch mode of the designated activity is "standard", it too is removed from the stack and a new instance is launched in its place to handle the incoming intent. That's because a new instance is always created for a new intent when the launch mode is "standard". – Alex Bonel Mar 27 '13 at 05:11
  • I tried almost every launchmode/flag combination and it still didn't work. The onDestroy() approach would stop playback - I don't want this. Edit: To make it clearer, said Player should play in background (more or less), if I hit the play button (in the library) again, I don't want a new Player, but get back to the old one. – AdmiralSnackbar Mar 27 '13 at 09:19