jul
jul

Reputation: 37474

Issue with activity called with Intent.FLAG_ACTIVITY_CLEAR_TOP

I want to finish my app calling the first activity with Intent.FLAG_ACTIVITY_CLEAR_TOP and finishing it. However, when it finishes, the app restarts automatically, and goes directly to Activity 2.

Why? Isn't the stack of activity supposed to be empty after finishing an activity called with Intent.FLAG_ACTIVITY_CLEAR_TOP?

My stack is Activity2>(more activities)>Activity1.

In Activity2

Intent exit_intent=new Intent(context, Activity1.class);
exit_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
exit_intent.putExtra("EXIT", true);
context.startActivity(exit_intent);

In Activity1

if (getIntent().getBooleanExtra("EXIT", false)) {           
    finish();
}

Upvotes: 0

Views: 3308

Answers (5)

wangx
wangx

Reputation: 1

From Intent doc:

public static final int FLAG_ACTIVITY_CLEAR_TOP 

If it has declared its launch mode to be "multiple" (the default) and you have not set FLAG_ACTIVITY_SINGLE_TOP in the same intent, then it will be finished and re-created.

Upvotes: 0

Felix
Felix

Reputation: 89596

Are you sure finish() is being called properly in Activity1? I'm getting the feeling it's not, because:

  1. I'm not sure where that if statement goes inside your Activity. It should be in onNewIntent.
  2. If you are making that if statement inside the onNewIntent method, it's still wrong. The docs specify that getIntent() will always return the original intent that started the Activity, unless you call setIntent().

To conclude, maybe something else is getting called in your Activity1 (can't tell without the full code) that starts Activity2 instead of finishing.

If what I described is not the case, and your activity stack indeed looks like Activity2 > Activity1 like the others have described yes, it will not work. Just call finish() in Activity2?

Upvotes: 1

DArkO
DArkO

Reputation: 16110

You have it right but are you catching the intent inside onNewIntent method of your first activity? also activity 2 should be launched after activity 1 in order for this to work.

Upvotes: 0

kabuko
kabuko

Reputation: 36302

According to the docs:

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.

So it doesn't clear the entire activity stack, only any other activities that were on top of an old instance of the activity being launched.

Upvotes: 0

Femi
Femi

Reputation: 64700

From the javadoc:

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.

CLEAR_TOP will wipe out all Activities ABOVE Activity1: if Activity2 is below Activity1 then once Activity1 finishes you will see Activity2.

Upvotes: 6

Related Questions