1

How to oveeride on back press to go to the main launcher or exit the application? But not kill it

android_king22
  • 759
  • 2
  • 20
  • 36
  • Couple of similar questions, take a look at the code here: [1]: http://stackoverflow.com/questions/3141996/android-how-to-override-the-back-button-so-it-doesnt-finish-my-activity [2]: http://stackoverflow.com/questions/2459848/android-prompt-user-to-save-changes-when-back-button-is-pressed – Vinay Aug 08 '11 at 20:33

1 Answers1

12

Override onBackPressed method of your activity:

@Override
public void onBackPressed() 
{
        moveTaskToBack(true);
}
peter.bartos
  • 11,855
  • 3
  • 51
  • 62
  • Do think about this answer for a while, though. How will your app exit? Exit buttons in Android apps are, generally, bad form. If you want to push an app back without exiting it, you can always hit the home button. – Earl Aug 08 '11 at 20:39
  • @Earl But what about the case where the developer does not want the app to run in the background. – Mike D Aug 08 '11 at 20:45
  • 1
    @Mike D: That's not what's being asked and answered here. In any event, both are possible with the default behavior. The home button will push the app back, calling onPause(), but leaving it running for at least a while. Hitting the back button will exit the app, calling onDestroy() (after onPause() and onStop()) and clean up. The question and solution are how to avoid the latter. I'm suggesting it shouldn't be avoided. If you want to leave something running in the background, use a service – Earl Aug 08 '11 at 20:50
  • With the answer above. The app isnt exited, but instead paused() Exactly what i want to happen. And only killed when memory needs too. – android_king22 Aug 08 '11 at 21:09