You can call finish()
in your activity to finish it.
There are flags which you can use in this time depending on your requirement. Here is how they work :
FLAG_ACTIVITY_CLEAR_TASK
- If set in any intent that is passed to your startActivity(), it will cause any existing task that would be associated with the activity to be cleared before the activity is started. That is, all old activities are finished.
FLAG_ACTIVITY_CLEAR_TOP
- If set in any intent that is passed to your startActivity(), and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the old activity as a new Intent.
FLAG_ACTIVITY_NEW_TASK
- If set in any intent that is passed to your startActivity(), this activity will become the start of a new task on this history stack.
FLAG_ACTIVITY_SINGLE_TOP
- If set in any intent that is passed to your startActivity(), the activity will not be launched if it is already running at the top of the history stack.
You can use it like this:
Intent i=new Intent(this, Sample.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
For further clarifications you can check this Intents and also Back Stack and Tasks