1

Hello my application works like this now..

Main -> SEARCH -> ActivityA -> SEARCH -> ActivityB

And when I click back from ActivityB, I want to do the following

Main <- SEARCH <- ActivityB

i.e I want to skip activities ActivityA and SEARCH. I know I have to use the FLAG, but how?

Rohith Nandakumar
  • 11,367
  • 11
  • 50
  • 60
  • http://stackoverflow.com/questions/5790973/remove-start-activity-from-the-history/5791116#5791116 – Zelimir Apr 27 '11 at 09:54

3 Answers3

2

may be this be helpful Android: Clear the back stack

Community
  • 1
  • 1
1

You coud just do:

In SearchActivity

Intent i = new Intent(this, ActivityA.class);
i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(i);

In ActivityA

Intent i = new Intent(this, SearchActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(i);

which will take you back from B to Search and then to Main on two presses of the back button.

However

If you go from Main to Search to A to Search and then hit the back buttons, you would go from Search to Search to Main. (Two instances of search, probably not what you want)

It's better to set the flags in Activity A to be:

Intent i = new Intent(this, SearchActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);

This will stop the above behaviour and still give you what you want when you hit back from B

NickT
  • 23,844
  • 11
  • 78
  • 121
0

I just had to override the back button in SEARCH activity like so

 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event)  {
     if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
         Log.d(TAG, "back pressed");
         Intent intent = new Intent(this, MainActivity.class);
         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         startActivity(intent);
     }
     return super.onKeyDown(keyCode, event);
 }
Rohith Nandakumar
  • 11,367
  • 11
  • 50
  • 60