2

I am working on an Android application which has logout button. When I click on that I need to close my app completely (I don't want to run the app in background as well).

I tried with System.exit(0) and finish() as well.

halfer
  • 19,824
  • 17
  • 99
  • 186
Sekhar Bhetalam
  • 4,501
  • 6
  • 33
  • 52

3 Answers3

4

on your logout button try this code:

 finish();
 moveTaskToBack(true);
 System.exit(0);

i think this will not close your app completely but will exit from all of the activities.

Shiv
  • 4,569
  • 4
  • 25
  • 39
0

Android has a mechanism in place to close an application safely.

In the last Activity on the stack (usually the main, or just the first you have started) override the onDestroy() method.

You can either call the System.runFinalizersOnExit(true) which ensures that all objects will be finalized and garbage collected when the the application exits, or kill an application quickly via android.os.Process.killProcess(android.os.Process.myPid()) if you prefer.

0

the finish()-method runs the destruction of the activity. so, it should be right here.

bofredo
  • 2,348
  • 6
  • 32
  • 51