28

I have a common menu on my app with icons. Clicking an icon will start an Activity. Is there a way to know if an activity is already running and prevent it from starting multiple times (or from multiple entries)? Also can I bring an activity that is in onPause state to the front?

XurajB
  • 820
  • 1
  • 14
  • 29
  • I haven't done Android development in a while, but I'm pretty sure that Android will bring up an existing instance of your `Activity` instead of starting a new one. – Robert Rouhani Jan 18 '12 at 07:16

7 Answers7

57

Use this:

intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

while starting Activity.

from documentation:

If set in an Intent passed to Context.startActivity(), this flag will cause the launched activity to be brought to the front of its task's history stack if it is already running.

Adil Soomro
  • 37,609
  • 9
  • 103
  • 153
19

In your activity declaration in Manifest file, add the tag android:launchMode="singleInstance"

Janusz
  • 187,060
  • 113
  • 301
  • 369
AAnkit
  • 27,299
  • 12
  • 60
  • 71
  • From the documentation of startActivityForResult, if the activity you are launching uses the singleTask launch mode, it will not run in your task and thus you will immediately receive a cancel result. – Zain Ali Aug 03 '13 at 06:27
  • 1
    in fact I'm not a big fan of "singleInstance" as it cause the activity to run in a separate window as if it is a separate app which give a very bad user experience when the user opens the apps view. – Muhammed Refaat Apr 09 '17 at 08:08
5

I got it perfectly working by doing the following. In the caller activity or service (even from another application)

Intent launchIntent = getPackageManager().getLaunchIntentForPackage(APP_PACKAGE_NAME);

//the previous line can be replaced by the normal Intent that has the activity name Intent launchIntent  = new Intent(ActivityA.this, ActivityB.class);

        launchIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT|Intent.FLAG_ACTIVITY_NEW_TASK);

startActivity(launchIntent);

and in the manifest of the receiver activity (the I want to prevent opening twice)

  <activity android:name=".MainActivity"
            android:launchMode="singleTask"
            >
MSaudi
  • 4,442
  • 2
  • 40
  • 65
1

This works for me :

   intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

from official documentation

If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.

also, you can use FLAG_ACTIVITY_NEW_TASK with it.

then the code will be :

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
Nik
  • 113
  • 1
  • 5
0

Just use

Intent i = new Intent(ActivityA.this, ActivityB.class);
i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
user4292106
  • 441
  • 1
  • 11
  • 24
-1

please add this in menifest file

<activity
android:name=".ui.modules.profile.activity.EditProfileActivity"
android:launchMode="singleTask" // <<this is Important line 
/>
shashank J
  • 67
  • 7
  • 2
    Please add some explanation to your answer. – ItamarG3 May 19 '17 at 12:21
  • **From review queue:** May I request you to please add some more context around your answer. Code-only answers are difficult to understand. It will help the asker and future readers both if you can add more information in your post. – help-info.de May 19 '17 at 17:46
-1

create an instance of your activity which you dont want to start multiple times like

Class ExampleA extends Activity {
 public static Activity classAinstance = null;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    classAinstance = this;
}

}

Now where ever u want to crosscheck i mean prevent it from starting multiple times, check like this

if(ExampleA.classAinstance == null) {
       "Then only start your activity"
}
Arun
  • 1,658
  • 1
  • 14
  • 21
  • 2
    Beware that this will prevent the Activity from being garbage collected. Additionally, this will not help if the Activity is started from some other component (e.g. another app) that can't access (or even know about) the static Activity reference. – lxgr Jun 08 '15 at 15:54