Finish all previous activities


My application has the following flow screens :

Home->screen 1->screen 2->screen 3->screen 4->screen 5

Now I have a common log out button in each screens

(Home/ screen 1 / screen 2 /screen 3/ screen 4 / screen 5)

I want that when user clicks on the log out button(from any screen), all the screens will be finished and a new screen Log in will open .

I have tried nearly all FLAG_ACTIVITY to achieve this. I also go through some answers in stackoverflow, but not being able to solve the problem. My application is on Android 1.6 so not being able to use FLAG_ACTIVITY_CLEAR_TASK

Is there any way to solve the issue ?

9/13/2016 6:50:57 AM

Accepted Answer


Intent intent = new Intent(getApplicationContext(), Home.class);

This will clear all the activities on top of home.

Assuming you are finishing the login screen when the user logs in and home is created and afterwards all the screens from 1 to 5 on top of that one. The code i posted will return you to home screen finishing all the other activites. You can add an extra in the intent and read that in the home screen activity and finish it also (maybe launch login screen again from there or something).

I am not sure but you can also try going to login with this flag. I don't know how the activities will be ordered in that case. So don't know if it will clear the ones below the screen you are on including the one you are currently on but its definitely the way to go.

Hope this helps.

4/26/2018 8:37:53 PM

Before launching your new Activity, simply add the following code:


Or if you want it to work in previous versions of Android:


When the user wishes to exit all open activities, they should press a button which loads the first Activity that runs when your application starts, clear all the other activities, then have the last remaining activity finish. Have the following code run when the user presses the exit button. In my case, LoginActivity is the first activity in my program to run.

Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
intent.putExtra("EXIT", true);

The above code clears all the activities except for LoginActivity. Then put the following code inside the LoginActivity's onCreate(...), to listen for when LoginActivity is recreated and the 'EXIT' signal was passed:

if (getIntent().getBooleanExtra("EXIT", false)) {

Why is making an exit button in Android so hard?

Android tries hard to discourage you from having an "exit" button in your application, because they want the user to never care about whether or not the programs they use are running in the background or not.

The Android OS developers want your program to be able to survive an unexpected shutdown and power off of the phone, and when the user restarts the program, they pick up right where they left off. So the user can receive a phone call while they use your application, and open maps which requires your application to be freed for more resources.

When the user resumes your application, they pick up right where they left off with no interruption. This exit button is usurping power from the activity manager, potentially causing problems with the automatically managed android program life cycle.


Intent intent = new Intent(this, classObject);

This Will work for all Android versions. Where IntentCompat the class added in Android Support library.


Use the following for activity


remove CLEAR_TASK flag for fragment use.

I hope this may use for some people.


I guess I am late but there is simple and short answer. There is a finishAffinity() method in Activity that will finish the current activity and all parent activities, but it works only in Android 4.1 or higher.

For API 16+, use


For below 16, use


Hope it helps!


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]