I have activity A, B & C and I went from A-> C-> B-> at final activity I have one logout button, and on click of it, how to finish entire application with all previous activities and its stacks.
Asked
Active
Viewed 6,222 times
0
-
Check this http://stackoverflow.com/questions/8615431/close-all-running-activities-in-an-android-application/8615527#8615527 – Tufan Jan 27 '16 at 10:49
-
1Just call `Intent startMain = new Intent(Intent.ACTION_MAIN); startMain.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startMain.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); startMain.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); startMain.addCategory(Intent.CATEGORY_HOME); startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(startMain);` – IntelliJ Amiya Jan 27 '16 at 11:01
4 Answers
2
Try this,
Intent intent = new Intent(MainActivity.this, LoginActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
If you have session, so please clear your session before your intent have been called.

Parth Bhayani
- 1,894
- 3
- 17
- 35
-
As you can see, that will go (from `MainActivity`) to `LoginActivity` but, with these flags: `FLAG_ACTIVITY_CLEAR_TOP` or etc, for an `Activity`, not for an `Application`. – ʍѳђઽ૯ท Jan 27 '16 at 10:58
-
you also need to pass more two flags with intent will help you out. – Parth Bhayani Jan 27 '16 at 10:59
-
Yes, but See: `FLAG_ACTIVITY_NEW_TASK` , `FLAG_ACTIVITY_CLEAR_TASK` this is for an activity which you finished it with `finish();` means, something like restarting an `Activity` not an `Application`.and look at the title: how to **close entire application** with logout button – ʍѳђઽ૯ท Jan 27 '16 at 11:01
0
The answer is simple, you have to finish()
all of your active intents
.
So either make sure you only have 1 intent open at the time, or finish multiple intents at the final activity

Mathieu Brouwers
- 564
- 7
- 19
-
`finish();` will close an `Activity` , but will certainly not close the entire app with certainty - refer to : http://stackoverflow.com/a/32570741/4409113 – ʍѳђઽ૯ท Jan 27 '16 at 10:54
-
1Hmm, I didn't know that. But the refered SO seems like a nice answer to this question. – Mathieu Brouwers Jan 27 '16 at 10:54
-
Yeah, look at this answer from CommonsWare too: http://stackoverflow.com/a/2034238/4409113 – ʍѳђઽ૯ท Jan 27 '16 at 10:55
0
The best solution is:
getActivity().finish();
System.exit(0);
Take a look at this question:
How to quit android application programmatically
And also: Is quitting an application frowned upon?
Which says:
but Android can very well totally destroy your app whenever it feels like it".. This is true for all modern OSs
So i think that is not a good way, though.