Reputation: 14370
I need to close an application through a button click. How to do this?
Upvotes: 1
Views: 1463
Reputation: 612
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);
Upvotes: 0
Reputation: 105
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.
Upvotes: 2
Reputation: 39
this work for me
Boolean
variable, for example (sysExit)
on 1st activity = false
onResume()
in every activityif(sysExit) finish();
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());
}
}
Upvotes: 1
Reputation: 634
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();
}
Upvotes: -1
Reputation: 1103
You need to call finish() method on onClick() method of the of Button.
Upvotes: 0
Reputation: 43108
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.
Upvotes: 2
Reputation: 5381
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.
Upvotes: 0