3

This answer recommends using AndroidManifest's android:launchMode="singleTask" to limit an activity to a single instance.

That's great for applications in which all activities are part of the project, but when I have several application using a Library Project in which the application's main activity is defined, the Library Project's AndroidManifest is ignored and so I can't really use that android:launchMode="singleTask" attribute there.

If I use that android:launchMode="singleTask" in the application's AndroidManifest.xml, then each activity has a different name and this attribute will limit to a single instance only the same application, not different applications using this shared activity (used as a Library Project).

What I really want is that at any given moment, only one application (among the different applications using that shared Library Project's activity) can run.

Is this doable using the android:launchMode="singleTask" attribute?

If this isn't possible, what other approach is recommended?

Community
  • 1
  • 1
ateiob
  • 9,016
  • 10
  • 44
  • 55

2 Answers2

3

Try using Intent.FLAG_ACTIVITY_REORDER_TO_FRONT. Whenever you launch an Activity, if an instance of that Activity exists, it is brought to the front of the stack and not created the second time. This ensures that only one Activity remain on the stack at any given moment.

Another approach is to use a static variable. Not sure how reliable this is though.

Community
  • 1
  • 1
scatmoi
  • 1,958
  • 4
  • 18
  • 32
  • The `Intent.FLAG_ACTIVITY_REORDER_TO_FRONT` doesn't help me because I don't launch my application's activity programmatically. It's all done via the manifest file. The static variable approach is more suitable but @AlexLockwood's answer provides more information via the link. Thanks. – ateiob Jul 31 '12 at 03:35
2

The Library Project is just something you use to re-use code. When the application is compiled, the resulting .apk file doesn't know about the Library Project that helped create it. That said, it shouldn't be possible to enforce the singleton property (across different .apk's) by manipulating the Library Project's AndroidManifest.xml file.

You might be able to enforce this behavior by subclassing Application. You can read more about this here.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
  • 1
    Thanks for the link. Interesting answer indeed. Not sure whether "*visible*" is the same as "*running*", though. – ateiob Jul 02 '12 at 15:57
  • I'm curious... whats the reason for preventing multiple apps from running anyway? (Not saying it's wrong necessarily... I'm just curious). – Alex Lockwood Jul 02 '12 at 16:01
  • They all play MP3 files which results in a cacophony when 2 or more such applications run at the same time. I can probably manage access to the shared speaker/earphones or media player, but at this point I just prefer to present a friendly message to the user saying that another application of this type is running. – ateiob Jul 02 '12 at 16:32
  • Setting a static boolean in onResume/onPause is straightforward but why do I need to subclass Application? I can just keep that static boolean in the base activity. Also, once I can determine an instance is already running, where do I stop the 2nd instance from starting? `killProcess`? finish? Other? – ateiob Jul 31 '12 at 03:39
  • I honestly can't give you a 100% confident answer on how to close the 2nd application... it seems like really odd behavior, and Android definitely wasn't designed with the explicit idea of closing other third party applications in mind. Your best bet would probably be `ActivityManager#killBackgroundProcesses(String packageName)` though... at least that would be my first guess. :) – Alex Lockwood Jul 31 '12 at 03:47