I'm building a pizza order app. When the user adds an item to his cart, he is given an option to go to the item categories and browse/add new items. What I want to do is:
Main Screen > CategoryList(first appearance) > Pizzas in that category > Pizza Details > Shopping Cart > CategoryList(second apperance)..
I need to erase the Activity stack so that after adding an item to the cart, if the user touches the back button, he/she should go back to CategoryList(first appearance). "Pizzas in that category > Pizza Details > Shopping Cart" must be erased. If he/she touches back again, he/she should be at Main Screen.
How can I accomplish this?
You could try to push the latest activity using intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
or intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
see
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP
So then you clear the stack of activities under your new activity.
Additionally you could try to override the back-event:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
see also Android: Clear Activity Stack or How to clear the Android Stack of activities?