Finish all activities at a time

136,444

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.

Share:
136,444
steve
Author by

steve

Updated on July 05, 2022

Comments

  • steve
    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
    gumuruh almost 10 years
    but 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
    Akarsh M almost 10 years
    system.exit(0) for exit from the application : perfect for close Activity from Broadcastreceiver
  • Pijusn
    Pijusn over 9 years
    Never 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
    ezaquarii over 9 years
    Global variables are serious antipatterns.
  • User
    User over 9 years
    What does putExtra do?
  • Erum
    Erum over 9 years
    @letsnurture activity will remain in recent apps list
  • CoDe
    CoDe about 9 years
    What can do while launching form Receiver. I tried same you suggest but it's not working...any suggestion?
  • Jemshit Iskenderov
    Jemshit Iskenderov over 8 years
    did not work, still has an activity which has not been destroyed
  • Admin
    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
    Narender Gusain over 8 years
    getIntent() 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
    Kevin Lee over 8 years
    Thanks 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
    Chris Ho about 8 years
    Need return after finish(), else u will get a looping start activity : if (getIntent().getBooleanExtra("EXIT", false)) { finish(); return; }
  • Pang
    Pang over 7 years
    For older versions of Android, use ActivityCompat#finishAffinity().
  • RBT
    RBT over 7 years
    May 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
    lacas over 7 years
    nice, this is the best
  • Veneet Reddy
    Veneet Reddy about 7 years
    Shouldn't this more ideally be handled in FirstActivity's onNewIntent?
  • PerracoLabs
    PerracoLabs over 6 years
    Terrible. Must never keep a global reference to an activity.
  • StepanM
    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
    Andrew Prock about 6 years
    Please 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
    Dante about 6 years
    Intent intent = new Intent(getApplicationContext(), LoginActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); startActivity(intent);
  • Surendar D
    Surendar D almost 6 years
    Working awsome... Thanks
  • Laimiux
    Laimiux almost 6 years
    This seems to be the best answer
  • Mr.G
    Mr.G over 5 years
    But 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
    ghchoi almost 5 years
    Does this finish ALL activities...? Or, only the current activity?
  • Himanshu Sharma
    Himanshu Sharma almost 5 years
    All activities running because of app.
  • lukassos
    lukassos almost 5 years
    best answer nowadays thanks to lifecycle activities, no need for control over their state or intents
  • MEGHA DOBARIYA
    MEGHA DOBARIYA almost 3 years
    thank you sir for help me to sort out this.
  • Jaimin Modi
    Jaimin Modi almost 3 years
    What If I clear all previous opening Activity and remain current activity open ?
  • Hayasiiiint
    Hayasiiiint over 2 years
    This worked for me! Thanks!