Daniel L.
Daniel L.

Reputation: 5140

Prevent multiple instances of my Android application composed of a single activity

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).

Upvotes: 25

Views: 22775

Answers (3)

Hunter S
Hunter S

Reputation: 1311

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.

Upvotes: 15

Mr Chops
Mr Chops

Reputation: 191

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.

Upvotes: 0

FoamyGuy
FoamyGuy

Reputation: 46856

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.

Upvotes: 42

Related Questions