I need to close an application through a button click. How to do this?
8 Answers
It is a very late response, but this is a perfect solution.
Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
startActivity(i);
android.os.Process.killProcess(android.os.Process.myPid());
Android will take you to the menu and you close the application. easy.

- 105
- 2
- 10
Basically, android model doesn't consider manual exiting the applications. They are keep in memory as long as resources allow system to do that. But if want force the application to exit you can call finish() on your current activity, but you won't see any difference for your application with clicking back or home button.

- 42,730
- 18
- 77
- 103
this work for me
- create static
Boolean
variable, for example(sysExit)
on 1stactivity = false
- override
onResume()
in every activity - inside onResume add
if(sysExit) finish();
- on exit button access the Boolean and make it true then finish this activity
this will close all in the stack till application exit ;)
protected void onResume()
{
// TODO Auto-generated method stub
super.onResume();
if (app_exit)
{
finish();
Process.killProcess(android.os.Process.myPid());
}
}

- 39
- 3
To close Android Application what i do was adding android:launchMode="singleTask" to my main activity in the maniphest...and then this code when i want to close the app
System.runFinalizersOnExit(true);
System.exit(0);

- 582
- 2
- 15
- 32
You can call the method finish()
, this will finish the current activity.
Note that this is exactly the same thing as pressing the back button.

- 4,676
- 3
- 24
- 32
You can use:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.button);
Button Button1 = (Button) findViewById(R.id.ButtonCloseAbout);
Button1.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
finish();
}

- 634
- 1
- 7
- 11