-1

I have created an Android app.

I need to close or exit my application when I click the back button from my mobile.

How can I achieve that?

Lucifer
  • 29,392
  • 25
  • 90
  • 143
prabu
  • 717
  • 3
  • 12
  • 30

6 Answers6

5

you have to handle the back button functionality

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();
    finish();
}
Preet_Android
  • 2,332
  • 5
  • 25
  • 43
3

When you press back button activity is popped form the stack and destroyed. The previous activity in the stack takes focus.

Suppose your have 3 activities. A, B and C. You navigate to C. A to B to c. From C you can navigate to A using the below code.

You can override the back button pressed and call finish().

If you are in activity A you can simply press back button to exit.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
    onBackPressed();

}

return super.onKeyDown(keyCode, event);
  }

 public void onBackPressed() {
Intent myIntent = new Intent(C.this, A.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//clear the backstack
startActivity(myIntent);
finish();
return;
   }

Edit:

Some developers and some people on stackoverflow think that back button should atleast go back to the previous activity. It is meant to be like that. So overriding the default functionality and clearing back stack might not be a good idea.

You may also want to consider using Navigation Drawer

http://developer.android.com/design/patterns/navigation.html

Also check this

Is quitting an application frowned upon?

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
2
@Override
public void onBackPressed() {
  this.finish();
}

Try this

Say if you are in Some inner activity set some boolean on Application Class and check for that boolean in resume of every other activity then finish it if the boolean is set

Arun C
  • 9,035
  • 2
  • 28
  • 42
1

You don't need to invoke any method to close the app; Android takes care of it. Whenever you press the back button, finish() is called on that Activity and the activity is destroyed. You are not asking about Service right? Because a service runs in the background and you have to take care of closing the service according to your need.

Vikram Gupta
  • 6,496
  • 5
  • 34
  • 47
1

Pleeeease read this answer by CommonsWare: Is quitting an application frowned upon?

It's a very good breakdown on why this is not a good approach for Android app design. Concluding:

Similarly, I would counsel you against attempting to port your application to the Web, since some of the same problems you have reported with Android you will find in Web apps as well (e.g., no "termination"). Or, conversely, someday if you do port your app to the Web, you may find that the Web app's flow may be a better match for Android, and you can revisit an Android port at that time.

Community
  • 1
  • 1
Mike
  • 1,000
  • 1
  • 10
  • 18
-1

Try this.

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) {

        finish();
    }
    return false;
}
Yugesh
  • 4,030
  • 9
  • 57
  • 97