10

Possible Duplicate:
Removing an activity from the history stack

I am looking for a solution to remove the StartActivity from the history stack. The StartActivity is the one that is being started at the beginning. I am using this Class to check some user values and want to redirect the user to the MainActivity if all is correct.

I have tried to set a flag:

addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

but this does not do what it should.

App starts -> StartActivity -> MainActivity -> PRESS back -> the app should end

it does:

App starts -> StartActivity -> MainActivity -> PRESS back -> StartActivity

Thank for your help!

Edit: This is the code i am using to start the MainActivity:

Intent i = new Intent(context, DashBoardActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
Community
  • 1
  • 1
Mark
  • 7,507
  • 12
  • 52
  • 88

2 Answers2

19

You can simply call finish() after you start the MainActivity.

Intent i = new Intent(context, DashBoardActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
Flo
  • 27,355
  • 15
  • 87
  • 125
  • And of course this approach works as well, but when he mentioned removing from the history, flag was first that came to my mind. – Zelimir Apr 26 '11 at 13:53
  • Yes, your solution might be the proper way to archive it. My one is more the quick and dirty one. ;) – Flo Apr 26 '11 at 13:55
  • Thanks, your solution worked. But the other solution @Zelimir seems to be more elegant. – Mark Apr 26 '11 at 13:55
  • thanks, it wasn't immediately clear from the docs that you need to pass `FLAG_ACTIVITY_CLEAR_TOP` and ALSO call `finish()` – handler Jan 18 '15 at 00:49
10

You should use FLAG_ACTIVITY_NO_HISTORY when starting StartActivity to achieve described behaviour.

The same you may achieve if you set noHistory attribute to true in your AndroidManifest.xml.

Zelimir
  • 11,008
  • 6
  • 50
  • 45
  • note: this method doesn't work if you try to use a login provider like facebook (which launches it's own activity and then returns you back to your start activity afterwards) – handler Jan 18 '15 at 00:50
  • That's my situation, any tip how to deal with that ? – serj Apr 30 '15 at 10:19