I want to close my whole application when click on device's back button. How can I do this? Please help me.
thank you
I want to close my whole application when click on device's back button. How can I do this? Please help me.
thank you
That's one of most useless desires of beginner Android developers, and unfortunately it seems to be very popular. How do you define "close" an Android application? Hide its user interface? Interrupt background work? Stop handling broadcasts?
Android applications are a set of modules, bundled in an .apk and exposed to the system through AndroidManifest.xml
. Activities can be arranged and re-arranged through different task stacks, and finish()-ing or any other navigating away from a single Activity may mean totally different things in different situations. Single application can run inside multiple processes, so killing one process doesn't necessary mean there will be no application code left running. And finally, BroadcastReceivers can be called by the system any time, recreating the needed processes if they are not running.
The main thing is that you don't need to stop/kill/close/whatever your app trough a single line of code. Doing so is an indication you missed some important point in Android development. If for some bizarre reason you have to do it, you need to finish() all Activities, stop all Services and disable all BroadcastReceivers declared in AndroidManifest.xml
. That's not a single line of code, and maybe launching the Activity that uninstalls your own application will do the job better.
I think its not possible to close entire application. see these links it may help you.
How to exit an Android Application
It may help you check it.
android.os.Process.killProcess(android.os.Process.myPid())
call the moveTaskToBack() method inside the onKeyDown.
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
In this case, I'm using this code:
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK) {
android.os.Process.killProcess(android.os.Process.myPid());
return true;
}
return super.onKeyDown(keyCode, event);
}
It work for me.
Add this code on your activity When clicked android back button application closed but running in background! You can add finish() and System.exit(0)
@Override public void onBackPressed() { super.onBackPressed(); moveTaskToBack(true); }
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
return true; //not sure this is needed
}
return super.onKeyDown(keyCode, event);
}
You can Call finish(); in back button
whenever you starts an activity just put
finish();
before
startActivity(intent);
This is the way to close your application with back button.