0

Possible Duplicate:
android pressing back button should exit the app

I am trying to close an app using mobile's back button. Currently I am overriding onKeyPressed method and calling moveTaskToBack(true);. It does hides the app but does not close it. It still remains in the memory. How can I close the app by pressing the back button?

Here is my code

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        moveTaskToBack(true);
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
Community
  • 1
  • 1
Om3ga
  • 30,465
  • 43
  • 141
  • 221
  • This is correct behaviour. Why do you want to close your app? In nearly every situation, you should let Android handle memory management and app closing. – Simon Oct 02 '12 at 07:10
  • http://stackoverflow.com/questions/2033914/quitting-an-application-is-that-frowned-upon – Nermeen Oct 02 '12 at 07:12

8 Answers8

2

I think its safer to do this.finish(); instead.

System.exit(0) is not recommended, better let Android do the management of the app.

Check this link : Is quitting an application frowned upon?

Community
  • 1
  • 1
midhunhk
  • 5,560
  • 7
  • 52
  • 83
2

You don't. Well, not completely.

An app's lifecycle is made in such a way that being in the background is normal and expected. This allows apps to be quickly restored to the front if/when the user wants, and allows Android to do the memory management. You have special methods like onPause() and onResume() to help you handle this.

Beyond that, finish() will close the current activity and remove it from the stack, while System.exit(0) should close the app.

You should read an excellent answer by CommonsWare (Mark Murphy) to this question, as it addresses a number of reasons to let Android keep your app in the memory.

Community
  • 1
  • 1
Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
1

When you push the new activity, call finish() on the previous, otherwise it will remain on the stack, therefore appearing when you hit back and pop the current activity.

Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
1

You should be able to use finish() to close down your app. However if it's due to memory management, I'd rather use android's own memory management. It's actually quite good. It will periodically remove applications which are paused, if it's necessary to free up memory. And unless it's a service, it shouldn't take up too much memory while lying in the stack. There are exceptions however.

Martin
  • 157
  • 1
  • 11
1

Don't hold it to me but I believe onbackpressed is now the recommended method. And yes finish () ; should work just fine.

mango
  • 5,577
  • 4
  • 29
  • 41
0

- Use System.exit(0) to close you app at the back button, this is quite useful with java application, same works for Android, but then it would be advisable to use the below methods.

finish() and finishActivity() are 2 alternatives

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
0

If you want to exit application, write following:

finish() 

because System.exit(0) is not recommended. This will also exit from your application.

Community
  • 1
  • 1
Hardik Joshi
  • 9,477
  • 12
  • 61
  • 113
-1

System.exit(0) closes the entire app. finish() is another snippet that closes the current activity.

MrYanDao
  • 1,253
  • 1
  • 15
  • 27