I have an Android application which is composed from a single Activity
.
How can I assure that only one instance of my application (== Activity
) will exist in a given time?
I got to a situation in which I succeeded to open multiple instances of my app by clicking on the app icon multiple times (this doesn't reproduce all the time).

- 5,060
- 10
- 36
- 59
3 Answers
change your manifest like this:
<activity
android:name="com.yourpackage.YourActivity"
android:label="@string/app_name"
android:launchMode="singleTask" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
include the android:launchMode="singleTask"
and it will not be possible to have multiple instances of your Activity launched at the same time. See the activity docs for more.

- 46,603
- 18
- 125
- 156
-
4is it preferred over `"singleInstance"`? – Daniel L. May 22 '13 at 13:56
-
read the difference between the two in the docs and decide which one fits your use case better... Without knowing anything more about your application I can't answer that. – FoamyGuy May 22 '13 at 14:08
-
4android:launchMode="singleTask" not working android:launchMode="singleInstance" works – xevser Apr 23 '18 at 18:27
-
Thanks a lot!!! It's 2022 and I was facing the same issue where I implemented an app theme changing dialog box and every time when I relaunched the app (making sure that it was not in the RECENTS before opening it i.e. it is not running in the background), a new instance of the same activity used to create when the current theme did not match with the device's default theme and I tried a lot of methods to correct it, but nothing worked. Finally, I found this, and my issue is resolved now. Btw, both "singleTask" and "singleInstance" worked for me. : ) – Shubham Nanche Feb 23 '22 at 13:06
The accepted answer serves its purpose, but it is not the best way to do this.
Instead, I recommend using a static AtomicInteger
inside of each of your activities like so:
//create a counter to count the number of instances of this activity
public static AtomicInteger activitiesLaunched = new AtomicInteger(0);
@Override
protected void onCreate(Bundle pSavedInstanceState) {
//if launching will create more than one
//instance of this activity, bail out
if (activitiesLaunched.incrementAndGet() > 1) { finish(); }
super.onCreate(pSavedInstanceState);
}
@Override
protected void onDestroy() {
//remove this activity from the counter
activitiesLaunched.getAndDecrement();
super.onDestroy();
}
So, what's wrong with the accepted answer?
Declaring that your activity should be launched using the singleInstance
mode starts messing with the default behavior for activities and tasks, which can have some unwanted effects.
The Android docs recommend that you only disrupt this behavior when it's necessary (it's not in this case):
Caution: Most applications should not interrupt the default behavior for >activities and tasks. If you determine that it's necessary for your activity to modify the default behaviors, use caution and be sure to test the usability of the activity during launch and when navigating back to it from other activities and tasks with the Back button. Be sure to test for navigation behaviors that might conflict with the user's expected behavior.

- 1,293
- 1
- 15
- 26
-
Is the solution you propose supposed to work on apps with multiple Activities? I'm ending up with a blank Activity and in some scenarios the app won't start at all. – Luboš Staráček Jan 25 '18 at 15:46
-
It should. That is what I was using it for with no issue. Granted, that was a couple years ago... Is it possible that you have issues elsewhere in your code? Try to eliminate that possibility: Start a new project with two basic activities that only display text ("Activity 1" & "Activity 2") . Then implement this method & test in this control environment to see if it still gives you weird issues like that. – Hunter S Jan 26 '18 at 01:59
-
My use-case is to start my app from any 3rd party app with Intent.ACTION_VIEW action, while my app runs in background. Right, I will try it on some trivial code to see if it can work. – Luboš Staráček Jan 26 '18 at 08:42
-
-
1This should be the accepted answer. The singleTop launchMode caused a lot of problems in my app while receiving notifications. – Justin Ebby Aug 09 '18 at 10:49
-
1This answer solved my problem! Forgot about atomic integer for this purpose, this works fantastic – Gastón Saillén Dec 22 '18 at 20:20
-
Maybe I did this wrong, but this does not seem to prevent it be launched via an outside intent (i.e. opening an app URL) – foxtrotuniform6969 Nov 06 '20 at 20:49
-
@foxtrotuniform6969 I don't know if you did anything wrong because I don't know if the results you're getting are expected. I never tried doing that, but I would find it very surprising that launching the app from a URL changes the method in which its launched so drastically that this method no longer works. – Hunter S Nov 08 '20 at 02:18
-
@foxtrotuniform6969 Does this method work for you when not launching via an outside intent? – Hunter S Nov 08 '20 at 02:19
-
@HunterS As in, starting the main activity via an intent from the main activity? I'm using jetpack compose so I only have a single activity, so my use case may be niche (for now at least) – foxtrotuniform6969 Nov 08 '20 at 02:46
I've found users of my app have been running out of memory and I was struggling to work out why. While trying to do so I found I could open multiple instances of my app my repeatedly clicking the icon then Home, then the icon, then Home.. I could see the memory use going up and up until eventually the app crashed. Before it crashed I could click the Close menu option and the previous instance came to the front, this would happen as many times as I'd launched the app.
My solution was to add android:launchMode="singleInstance"
to the manifest. Since then I've been unable to open more than one instance or crash the app.

- 181
- 2
- 10