57

Lets try to explain my question: I got an application and a service. The application is started with activity A. The service sends a broadcast that will let the application start Activity B Now the user starts activity C.

Now the service wants to start activity B again. But how do I let him know that the activity is still on the stack, or is there an intent flag for this?

How do I avoid that it will launch activity B because its already in the stack?

Ion Aalbers
  • 7,830
  • 3
  • 37
  • 50

7 Answers7

82

I think you need to make your activity B singleInstance that if it's already create you don't want to create again, that is launch mode of the activity can be defined in manifest android:launchMode that defines how the activity will be instanciated.

in your case use android:launchMode="singleInstance"

MKJParekh
  • 34,073
  • 11
  • 87
  • 98
30

You can use flag Intent.FLAG_ACTIVITY_NEW_TASK. If the activity is already running it will bring that to front instead of creating new activity.

If you add Intent.FLAG_ACTIVITY_CLEAR_TOP with this, then all the activities after this activity in the backstack will be cleared.

Sadeshkumar Periyasamy
  • 4,848
  • 1
  • 26
  • 31
11

If the activity will be on the top if already started, set the FLAG_ACTIVITY_SINGLE_TOP flag

intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
mContext.startActivity(intent);
Pellet
  • 2,254
  • 1
  • 28
  • 20
  • This is escpecially usefull when using a sidebar/drawer and you don't wanta to open an activity twice when already in front. – dermatthias Dec 17 '15 at 15:54
  • I am starting an activity from a servi using Intent.FLAG_ACTIVITY_SINGLE_TOP, the activity does not start and I get this error: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? – yeahman Mar 10 '16 at 18:36
  • 1
    Exactly what I was looking for :) – cantona_7 Aug 22 '19 at 07:52
7

Approaches android:launchMode="singleInstance" and just adding flags to the Intent do not work for me. What works is that:

In the code where activity gets started:

Intent intent = new Intent(activity, ActivityThatHasToBeStarted.class);
intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
startActivity(intent);

In the ActivityThatHasToBeStarted:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
        finish();
        return;
    }
    // Code for this creation
}
Ahmed Salman Tahir
  • 1,783
  • 1
  • 17
  • 26
Andrew
  • 36,676
  • 11
  • 141
  • 113
4

You may consider using android:launchMode="singleTop" instead of android:launchMode="singleInstance" Good article about the differences

Gal Rom
  • 6,221
  • 3
  • 41
  • 33
2

If you don't need the second activity anymore, it is good practise to finish it, then you should do this on the second activity after the operation is ended:

startActivity(new Intent(this, FirstActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
finish();
Alex
  • 1,623
  • 1
  • 24
  • 48
-1

I would suggest you use Intent.FLAG_ACTIVITY_CLEAR_TOP as it will remove all activities which start on top of your targeted activity.

eg :

Intent intent = new Intent(sourceActivity, Target activity);

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

startActivity(intent)
Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147