I suggest you read about Task and Back Stack
To start, the actions in android are pushed onto the stack called a, if my memory serves me correctly, Activity Stack. When you start an action, you can add an intent flag that will affect the stack, for example
addFlags (Intent.FLAG_ACTIVITY_CLEAR_TASK)
I think I did what you are trying to do before (I'm not sure which flag I used, it was a long time ago: p). But just read about the activity task to find out which flag of intent fits what you are trying to do.
source share