How to dismiss previous activities in android at a certain point?
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 a开发者_高级运维pplication 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.
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);
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.
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.
精彩评论