1

I have activities A -> B -> C where A and B are for login and verification purpose. When a user reaches activity C, A and B are not necessary any more. That means if a user press BACK in C, the application should not go thru A and B, instead it should go back to HOME screen.

What's the conventional way to implement this in android? Thanks.

EDIT: to clarify a bit, user should be able to go back to A from B during login/verification phase, but not from C to A or B once the user reaches C.

llc
  • 129
  • 9

3 Answers3

3

When going to next activity call

finish();

before starting next activity.

Or, if not pressing back but going to next activity:

Intent intent = new Intent(this, A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Yar
  • 4,543
  • 2
  • 35
  • 42
  • 1
    I want user to be able to go back to A from B. I think I should not finish A before user passes B and reaches C. Sorry, but I updated the question for a specific requirement. – llc Aug 10 '11 at 08:52
  • Well, you can for example: redesign your app so that instead of an activity, a dialog message is displayed, OR just after the activity B, before finishing it and going to the activity C, call the activity A with some special parameter (in the intent) so that the activity A upon receiving it will finish immediately. And then the activity C will be called from B. – Yar Aug 10 '11 at 12:11
  • I agree with you Yar. I came up with the latter idea after some thinking. And redesign is a viable option, too. Thanks again. – llc Aug 11 '11 at 00:17
1

Using the following code:

    @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
        Intent data = new Intent(this, HomeScreenActivity.class);
        data.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(data);
        finish();
        return true;
    } else {
        return super.onKeyDown(keyCode, event);
    }
}

This way you override the back key behaviour and start a selected activity removing all other activities in between from the stack.

Bogdan M.
  • 1,726
  • 3
  • 15
  • 24
  • Thanks for the reply. Activity C is actually a TabActivity. It looks to me not a perfect idea to mess up with overriding back key behaviour in C. – llc Aug 10 '11 at 08:54
1

The simplest is to add

android:nohistory="true" 

in your manifest for Activity A and B.

Whether or not the activity should be removed from the activity stack and finished (its finish() method called) when the user navigates away from it and it's no longer visible on screen — "true" if it should be finished, and "false" if not. The default value is "false". A value of "true" means that the activity will not leave a historical trace. It will not remain in the activity stack for the task, so the user will not be able to return to it.

PravinCG
  • 7,688
  • 3
  • 30
  • 55
  • The problem is, user should be able to go back to A from B to change some info to complete the verification phase, but never from C to A or B. – llc Aug 10 '11 at 08:42
  • Thanks for the answer. P.S. not me voting down :) – llc Aug 10 '11 at 08:43
  • I believe there was nothing in the question to hint at this specific requirement. For this you can simply finish() as others have mentioned. Thanks for responding. – PravinCG Aug 10 '11 at 08:46
  • This helped my scenario, I didn't see this answer anywhere else. Thanks. – Matt Oct 30 '12 at 14:25