0

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.

hotzst
  • 7,238
  • 9
  • 41
  • 64
Prasad P
  • 1,042
  • 8
  • 5
  • Check this http://stackoverflow.com/questions/8615431/close-all-running-activities-in-an-android-application/8615527#8615527 – Tufan Jan 27 '16 at 10:49
  • 1
    Just 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 Answers4

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
1
this.finish();
System.exit(0);
Eranga Gamagedara
  • 536
  • 1
  • 3
  • 10
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
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.

Community
  • 1
  • 1
ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108