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

Daniel L. picture Daniel L. · May 22, 2013 · Viewed 17.7k times · Source

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

Answer

FoamyGuy picture FoamyGuy · May 22, 2013

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.