4

I want to exit entire app by clicking exit button. Actually I am using exit button instead of log-out. So whenever user clicks on it, it should finish complete app but it destroys only last running activity. What should i do now? I am using this method in my code, any help will be appreciated, thanks in advance.

         public void exit(View v){
                finish();
                System.exit(0);
            }

I have also used following code but it kills only last running activity and not the complete application.

             public void exit(View v){
                android.os.Process.killProcess(android.os.Process.myPid());
                System.exit(1);
            }
Abhishek Verma
  • 366
  • 2
  • 13
SparrOw
  • 109
  • 1
  • 1
  • 8
  • 1
    You should not do this unless you have a really good reason. It is anti-Android, annoys users and does nothing to help performance. All round bad idea. – Simon Jun 29 '14 at 12:03
  • yeah i have reason for that .. i know .. u are right but nothing is working in the way it should work :( – SparrOw Jun 29 '14 at 12:07

5 Answers5

9

There is an alternative for otherwise killing your app completely. Minimize it and the Android system will kill it automatically when necessary. To do so,

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

Furthermore, this SO question may help: How to close android app completely

Community
  • 1
  • 1
Abhishek Verma
  • 366
  • 2
  • 13
2

If you really, really have to, do this:

public void exit(View v){
    finish();
    android.os.Process.killProcess(android.os.Process.myPid());
}
HHK
  • 4,852
  • 1
  • 23
  • 40
  • Hans its taking me back to the previous activity .. i mean it is destroyng only one activity ,not the complete application :( – SparrOw Jun 29 '14 at 12:04
  • Ahh, now I see what you want. The activity back stack contains your old activities even after your app exits. If you exit your topmost activity and even kill the app the previous activity will be reopened. To avoid this you need to implement something like this: http://stackoverflow.com/a/19109602/414285 – HHK Jun 29 '14 at 12:11
  • Hans its not working for me .. :( dont know why may be there is some problem – SparrOw Jun 30 '14 at 10:11
1

If minimum sdk version is 21 and up.

finishAndRemoveTask()

So the app will be closed completely even from the task list.

SHISHIR
  • 321
  • 3
  • 4
0

Don't ever put an Exit button on an Android app.

Read here:

Closing Application with Exit button

Community
  • 1
  • 1
DavidBalas
  • 333
  • 7
  • 21
-2

I think the solution suggested from Hans Kratz is not the best one, because it may cause in problems since ressources will not be finalized first. Instead, you should kill your app safely by using the following code:

public void exit(View v){
     System.runFinalizersOnExit(true);
     System.exit(0);
}

But really think about it twice, because it is not the way Android apps usually work.

Robin
  • 709
  • 2
  • 8
  • 20
  • Yeah i know but time is too short that's why i was thinking about this ... this code is doing the same thing as that of Hans ... destroying only last running activity ... application is still running :( – SparrOw Jun 29 '14 at 12:06
  • Okay, now I see what you are looking for. Please look at the explanation of the accepted anser in the following post: http://stackoverflow.com/questions/2092951/how-to-close-android-application – Robin Jun 29 '14 at 12:33