Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 230
Daniel L. Avatar asked May 22 '13 13:05

Daniel L.


People also ask

How do I stop multiple instances of apps on Android?

include the android:launchMode="singleTask" and it will not be possible to have multiple instances of your Activity launched at the same time.

Can you run 2 instances of the same app Android?

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.

Can an app have multiple activities?

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.


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

like image 68
FoamyGuy Avatar answered Oct 22 '22 06:10

FoamyGuy


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.

like image 21
Hunter S Avatar answered Oct 22 '22 05:10

Hunter S