Reputation: 21
I have 5 activities: A, B, C, D and E.
I can start all activities from each of them, but I want that Back Stack only save the last activity.
For example: A->B->C->D. If I am in D, I can go back to C, but now, in C, when I push back button I want to only can go to the main activity (A).
Is it posible? Thanks!
Upvotes: 2
Views: 691
Reputation: 133580
You can clear the back stack once you are in C and navigate to A.
A to B.B to C. C to D.
Back D to C
In C you can use the below.
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
onBackPressed();
}
return super.onKeyDown(keyCode, event);
}
public void onBackPressed() {
Intent myIntent = new Intent(C.this, A.class)
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//clear top of stack.
startActivity(myIntent);
finish();
return;
}
Upvotes: 0
Reputation: 12534
When transitioning from B -> C, call the finish() method on Activity B. This will prevent B from being placed on the back stack.
Upvotes: 1