2

I want to close my whole application when click on device's back button. How can I do this? Please help me.

thank you

Shantha Kumara
  • 3,272
  • 4
  • 40
  • 52
user1061793
  • 1,047
  • 8
  • 19
  • 27
  • 2
    Why do you want to close the whole application? That's something which doesn't belong to the android lifecycle. Please reconsider your design because it smells very wrong: http://developer.android.com/reference/android/app/Activity.html – schlingel Dec 19 '11 at 12:48
  • 1
    possible duplicate of [Quitting an application - is that frowned upon?](http://stackoverflow.com/questions/2033914/quitting-an-application-is-that-frowned-upon) – CommonsWare Dec 19 '11 at 16:34

8 Answers8

2

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.

Shantha Kumara
  • 3,272
  • 4
  • 40
  • 52
Dany's
  • 943
  • 1
  • 7
  • 17
  • finish() closes single activity only, not entire application. System.exit(0) is ok but not good. – Yugandhar Babu Dec 19 '11 at 13:17
  • @YugandharBabu no, calling System.exit is *not* ok. It does not go along with Android's activity lifecycle – skynet Jul 27 '12 at 19:57
  • Application opens, detects there is no login to the web service and launches a new activity asking for login credentials. User hits back. Application detects there is no credentials and immediately re-launches the activity asking for login credentials. So, perhaps the login credentials dialog needs a way to NOT let the app go back to the main screen to finish running. So there are use cases where it's needed. One workaround is to use a shared preference that stores the fact you want to close the app, and check that in onResume on the main activity. – Tony Maro Dec 18 '12 at 17:18
1

I think its not possible to close entire application. see these links it may help you.

Close Application

How to exit an Android Application

It may help you check it.

android.os.Process.killProcess(android.os.Process.myPid())
Community
  • 1
  • 1
Yugandhar Babu
  • 10,311
  • 9
  • 42
  • 67
1

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);
}
kameny
  • 2,372
  • 4
  • 30
  • 40
0

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.

0

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);
}
Madi
  • 1,805
  • 1
  • 15
  • 9
0
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);
}
ikromm
  • 523
  • 6
  • 13
0

You can Call finish(); in back button

Arun Kumar
  • 877
  • 3
  • 13
  • 37
0

whenever you starts an activity just put

finish();

before

startActivity(intent);

This is the way to close your application with back button.

abhishek ameta
  • 194
  • 1
  • 12
  • I can't understand what you exactly want?? – abhishek ameta Dec 19 '11 at 12:55
  • 1
    The whole android framework is designed around a lifecycle (see link above). Misusing such methods (for example if you have a activity for sending a sms finishing it on the click of the send button is OK) for existing apps is against the design principile. This is very bad for UX. As mentioned in the first link of Yugandhar. – schlingel Dec 19 '11 at 13:05