E
E
ertaquo2012-01-03 15:36:08
Android
ertaquo, 2012-01-03 15:36:08

Android: App close with back button

Can you please tell me how to close the application by pressing the "back" button? Now I use the following code (in Activity):

  @Override
  public void onBackPressed()
  {
    moveTaskToBack(true);
    finish();
    System.runFinalizersOnExit(true);
        System.exit(0);
  }
  
  public void onDestroy() {
        super.onDestroy();

        System.runFinalizersOnExit(true);
        System.exit(0);
    }

But something tells me that this is not entirely true and redundant. The application is a game with libgdx, only one Activity is used.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sheremetat, 2012-01-03
@sheremetat

When you close the application with the back button, the onDestroy() method is called. To do this, intercepting the click of this button in the onBackPressed() method does not make sense. It is enough to do this: It makes sense to intercept the pressing of the “Back” button when it is necessary to implement a confirmation of the exit from the game or something like that.
public void onDestroy() {
moveTaskToBack(true);
super.onDestroy();
System.runFinalizersOnExit(true);
System.exit(0);
}

Y
YoungSkipper, 2012-01-03
@YoungSkipper

If you do not inherit from Application, and as a result do not use another Context other than the Context of your Activity, then just finish () is enough. Well, plus, of course, if you create threads yourself, then it’s better to explicitly delete them.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question