I've been trying to show a "Do you want to exit?" type of dialog when the user attempts to exit an Activity.
However I can't find the appropriate API hooks. Activity.onUserLeaveHint()
initially looked promising, but I can't find a way to stop the Activity from finishing.
IF you are using fragment instead of activity put this code in your main activity
In Android 2.0+ this would look like:
In earlier versions it would look like:
I'd prefer to exit with double tap on the back button than with an exit Dialog.
In this solution, it show a toast when go back for the first time, warning that another back press will close the App. In this example less than 4 seconds.
Token from: http://www.androiduipatterns.com/2011/03/back-button-behavior.html
Using Lambda:
You also need to set level language to support java 8 in your gradle.build:
in China, most App will confirm the exit by "click twice":