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).
include the android:launchMode="singleTask" and it will not be possible to have multiple instances of your Activity launched at the same time.
Running multiple instances of an app lets you use multiple accounts for those apps on your phone. With this feature now built into your system, you don't need to go hunting for app-cloning tools. You can now do that by simply toggling a switch on your Android device, as shown above.
Most apps contain multiple screens, which means they comprise multiple activities. Typically, one activity in an app is specified as the main activity, which is the first screen to appear when the user launches the app. Each activity can then start another activity in order to perform different actions.
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.
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(); }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With