Prevent opening Activity for multiple times

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.


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"
            >

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