Exit app when press back key on phone


#1

Anyone can help me, how to exit app when press back button with confirmation dialog box.


#2

If you want to exit the application when the user confirms they really mean to close it, you can do this by checking to see if there are any views to actually go back to. If there aren’t you can alert the user and close the application when they click yes.

My example below is specific to a native hybrid app running in cordova, and uses the cordova notifications plugin to prompt the user.

$ionicPlatform.registerBackButtonAction(function () {
    var previousView = $ionicHistory.backView();
    if (!previousView) {
        navigator.notification.confirm(
            "Are you sure you want to exit?",
            function(buttonIndex) {
                if (buttonIndex === 1) {
                    ionic.Platform.exitApp();
                }
            },
            'Confirm Exit',
            ['Yes', 'No']
        );
    }
});