How to Prevent Multiple Instances of an Activity When It Is Launched with Different Intents

Android: how to prevent multiple instances of an activity to be launched from a widget?

Try using:

openAppIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

description here.


You can also use:

openAppIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);

description here.

How to prevent multiple instances of app/activity when it is started from Intent.createChooser()?

Normally, if another application launches your Activity (via ACTION_SEND, for example), your Activity will be launched into the existing task of the other app. So if you use 5 other apps and each of them launch your Activity using ACTION_SEND, you will have 5 instances of your Activity, each in a separate task.

If you want your Activity to run by itself, in its own task, and not in the other app's task, then you need to specify launchMode="singleTask" in the <activity> declaration in the manifest for this Activity. Then, when another app launches your Activity, the Activity will be launched in a separate task. If there is already an instance of the Activity running in that task, then Android will not create a new instance of the Activity, it will just call onNewIntent() and deliver the Intent that the other app used in the call to startActivity().

How to avoid multiple instances of same Activity?

Setting either the following flags may help you to resolve your issue:

  • Intent.FLAG_ACTIVITY_CLEAR_TOP
  • Intent.FLAG_ACTIVITY_REORDER_TO_FRONT

How to prevent multiple instances of an application in recent app screen while using multiple tasks?

If you really need to create separate tasks (which I doubt, this is a very rare case), then you need to do the following:

  • If you want each task to show up in the list of recent tasks, then you will need a way for the user to differentiate. To do this you should provide a different icon for each <activity> (using android:icon="") and/or a different label for each <activity> (using android:label="").

  • If you only want one task to show up in the list of recent tasks, then you need to set the following on the other <activity> tags in the manifest:

    android:excludeFromRecents="true"

How to prevent multiple instances of same app caused by deep link?

Try changing your launchmode to singleTask.

Android docs say:

The system creates the activity at the root of a new task and routes
the intent to it. However, if an instance of the activity already
exists, the system routes the intent to existing instance through a
call to its onNewIntent() method, rather than creating a new one.

Another solution could be singleInstance.
Android docs say:

Same as "singleTask", except that the system doesn't launch any other
activities into the task holding the instance. The activity is always
the single and only member of its task.

Same or different instance when starting same activity from various places in app?

Depend on the launch mode you set on the activity. It can be standard, singleTop, singleTask or singleInstance. This link may help:

http://www.intridea.com/blog/2011/6/16/android-understanding-activity-launchmode

By default, the launch mode is Standard which means Android will create multiple instances of the activity.



Related Topics



Leave a reply



Submit