How to use Intent Flags in android?

How can we finish an activity and alternatively ?

To finish an activity you need to call finish() Method of activity either manually or press back button which itself calls finish() method.

I guess you are asking about Android launch mode that can also be declared using the Intent flags, such as :

1) FLAG_ACTIVITY_NEW_TASK - If set, this activity will become the start of a new task on this history stack. A task (from the activity that started it to the next task activity) defines an atomic group of activities that the user can move to. Tasks can be moved to the foreground and background; all of the activities inside of a particular task always remain in the same order.

2) FLAG_ACTIVITY_CLEAR_TOP - If set, 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 (now on top) old activity as a new Intent.

3) FLAG_ACTIVITY_SINGLE_TOP - If set, the activity will not be launched if it is already running at the top of the history stack.

More information of Intents is available in Android Developers website.

Also you can read a detailed description with examples in this link.

How to manipulate the activity stack with the help of Flags.

Manipulation of back stack depends on your requirement for e.g. if you want you see a certain activity later on after application starts then you can keep it in back stack Also if you do not want to see a definite screen for e.g. splash screen which is called only once needs to be finished while navigating to other screen.


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