Finish all activities at a time
Solution 1
Whenever you wish to exit all open activities, you should press a button which loads the first Activity that runs when your application starts then clear all the other activities, then have the last remaining activity finish. to do so apply the following code in ur project
Intent intent = new Intent(getApplicationContext(), FirstActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);
The above code finishes all the activities except for FirstActivity. Then we need to finish the FirstActivity's Enter the below code in Firstactivity's oncreate
if (getIntent().getBooleanExtra("EXIT", false)) {
finish();
}
and you are done....
Solution 2
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
finishAffinity();
For lower (Android 4.1 lower), use
ActivityCompat.finishAffinity(YourActivity.this);
Solution 3
The best solution i have found, which is compatible with devices having API level <11
Intent intent = new Intent(getApplicationContext(), HomeActivity.class);
ComponentName cn = intent.getComponent();
Intent mainIntent = IntentCompat.makeRestartActivityTask(cn);
startActivity(mainIntent);
This solution requires Android support library
Solution 4
For API 16+, use
finishAffinity();
For lower, use
ActivityCompat.finishAffinity(YourActivity.this)
Solution 5
There are three solution for clear activity history.
1) You can write finish()
at the time of start new activity through intent.
2) Write android:noHistory="true"
in all <activity>
tag in Androidmanifest.xml file, using this if you are open new activity and you don't write finish() at that time previous activity is always finished, after write your activity look like this.
<activity
android:name=".Splash_Screen_Activity"
android:label="@string/app_name"
android:noHistory="true">
</activity>
3) write system.exit(0)
for exit from the application.
![steve](https://i.stack.imgur.com/JVlDz.jpg?s=256&g=1)
steve
Updated on July 05, 2022Comments
-
steve almost 2 years
I have an application with multiple pages i.e., multiple activities and some of them remain open.
Is there a way to close all activities at once?
-
gumuruh almost 10 yearsbut this code, it will make your "Back" button no longer working as usual. It will close everything when you click "Back" button. So how to deal with that / @letsnurture ?
-
Akarsh M almost 10 yearssystem.exit(0) for exit from the application : perfect for close Activity from Broadcastreceiver
-
Pijusn over 9 yearsNever hold static references to activities. Activities use relatively a lot of memory which should be freed up when activities go to background. static references are held as long as process is alive which application-wise means forever. It's both bad for you as the developer and the user.
-
ezaquarii over 9 yearsGlobal variables are serious antipatterns.
-
User over 9 yearsWhat does
putExtra
do? -
Erum over 9 years@letsnurture activity will remain in recent apps list
-
CoDe about 9 yearsWhat can do while launching form Receiver. I tried same you suggest but it's not working...any suggestion?
-
Jemshit Iskenderov over 8 yearsdid not work, still has an activity which has not been destroyed
-
Admin over 8 years@Jemshit Iskenderov aren't destroyed because you need preserve sessions objects etc. if you want stop system(0) you can see in google.
-
Narender Gusain over 8 yearsgetIntent() will not work in Oncreate, because your FirstActivity is already created. So use onNewIntent(Intent data), it will help you to get intent on FirstActivity.....
-
Kevin Lee over 8 yearsThanks for your approach, it helped me. I did something similar using a static class variable instead of shared prefs, which survives the entire app lifespan. In each activity's onResume, call super.onResume(); if (ExitHelper.isExitFlagRaised) { this.finish(); }. While it might not finish ALL activities, It does finish activities that would be resumed automatically.
-
Chris Ho about 8 yearsNeed return after finish(), else u will get a looping start activity : if (getIntent().getBooleanExtra("EXIT", false)) { finish(); return; }
-
Pang over 7 yearsFor older versions of Android, use
ActivityCompat#finishAffinity()
. -
RBT over 7 yearsMay I request you to please add some more context around your answer. Code-only answers are difficult to understand. It will help the asker and future readers both if you can add more information in your post.
-
lacas over 7 yearsnice, this is the best
-
Veneet Reddy about 7 yearsShouldn't this more ideally be handled in
FirstActivity
'sonNewIntent
? -
PerracoLabs over 6 yearsTerrible. Must never keep a global reference to an activity.
-
StepanM about 6 years+ must check getIntent().getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY == 0, otherwise if launch app from recents, App finish() it self, because Intent.extra."EXIT" remains in Intent
-
Andrew Prock about 6 yearsPlease don't post identical answers to multiple questions. Post one good answer, then vote/flag to close the other questions as duplicates. If the question is not a duplicate, tailor your answers to the question.
-
Dante about 6 yearsIntent intent = new Intent(getApplicationContext(), LoginActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); startActivity(intent);
-
Surendar D almost 6 yearsWorking awsome... Thanks
-
Laimiux almost 6 yearsThis seems to be the best answer
-
Mr.G over 5 yearsBut this will cause not to add activity history to stack , which will cause to restart activity each time put it in to background to foreground
-
ghchoi almost 5 yearsDoes this finish ALL activities...? Or, only the current activity?
-
Himanshu Sharma almost 5 yearsAll activities running because of app.
-
lukassos almost 5 yearsbest answer nowadays thanks to lifecycle activities, no need for control over their state or intents
-
MEGHA DOBARIYA almost 3 yearsthank you sir for help me to sort out this.
-
Jaimin Modi almost 3 yearsWhat If I clear all previous opening Activity and remain current activity open ?
-
Hayasiiiint over 2 yearsThis worked for me! Thanks!