How can I close my Android application? After clicking the exit button. I tried the following code finish(); System.exit(0); but it only returns to the previous page. what should the code to go to the applications menu
-
You could find some work arounds. But quitting an app is considered very bad for user experience and [normally frowned upon.](http://stackoverflow.com/questions/2033914/quitting-an-application-is-that-frowned-upon) – Krishnabhadra Jul 18 '13 at 05:37
5 Answers
No. Android does not allows to exit an application. But you can use the following code to bring up the HOME screen:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

- 15,485
- 24
- 88
- 126
-
Do this kills the application? I think when we relaunches the app, it starts from where it left off. – Krishnabhadra Jul 18 '13 at 05:36
-
Android's does not allows exiting an application by choice, but it is managed by the OS. – Gokul Nath KP Jul 18 '13 at 05:39
-
*Android's does not allows exiting an application by choice, but it is managed by the OS* Yes, with good reasons too. Your answer just shows home screen on top of the existing app, which is very bad practice to follow. – Krishnabhadra Jul 18 '13 at 05:45
int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);
but it's not advisable..

- 47,782
- 38
- 107
- 158
This is not a good approach to exit directly from the application. You have to remove the every activity from Activity Stack
by using
youractivity.this.finish();
class A extends Activity
{
//like in this if u want to go to next activity by clicking a button, then in onClick() you have to write
A.this.finish();
}

- 4,206
- 8
- 36
- 65

- 10,224
- 2
- 37
- 59
First thing first, it is a very bad practice to exit the app programatically. You would rather let the OS handle it in normal way.
If you are insistent on doing this, you should do it this way.
- Call the first Activity in your app with flag
Intent.FLAG_ACTIVITY_CLEAR_TOP
, which finishes the currently on screen activity and all in between activities. - Now from the first activity, call
finish()
.
The advantage of doing this way, all activity life cycle method gets called, and the app cleans up its resources the proper way.

- 34,169
- 30
- 118
- 167
finish() will destroy the activity from which you called finish. so in your case you need to call android.os.Process.killProcess(android.os.Process.myPid()); it will kill all the process including all the activities on the stack that you started.

- 159
- 2
- 9