Answer the question
In order to leave comments, you need to log in
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);
}
Answer the question
In order to leave comments, you need to log in
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);
}
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 questionAsk a Question
731 491 924 answers to any question