This article describes the implementation method of Android return key function. Share it for your reference. The details are as follows:
When developing Android applications, you can often close the program by pressing the return key (i.e. keyCode == KeyEvent.KEYCODE_BACK). In fact, in most cases, the modified application is not closed.
We can do this. When the user clicks the custom exit button or return key (the action needs to be captured), we forcefully exit the application in onDestroy() or directly kill the process. The specific operation code is as follows:
public boolean onKeyDown(int keyCode, KeyEvent event) { // Press the return button on the keyboardif (keyCode == KeyEvent.KEYCODE_BACK) { new (this) .setMessage("Are you sure to exit the system?") .setNegativeButton("Cancel", new () { public void onClick(DialogInterface dialog, int which) { } }) .setPositiveButton("Sure", new () { public void onClick(DialogInterface dialog, int whichButton) { finish(); } }).show(); return true; } else { return (keyCode, event); } } @Override protected void onDestroy() { (); // Or the following method //(0); //It is recommended to use this (()); }
I hope this article will be helpful to everyone's Android programming design.