Geoffrey Lalloué
Geoffrey Lalloué

Reputation: 1464

exit cordova Windows 10 application

When i use my cordova app in the home page, i need to display message to user to ask him if he really want to exit app or not.
Than i need to exit app if user select "yes".

To do it, i use this code :

document.addEventListener('backbutton', function() {
    $rootScope.back();
    $rootScope.$apply();
}, false);

$rootScope.back = function(execute) {
    var path = $location.$$path;
    if (path !== '/') {
        $location.path(path.split('/').slice(0, -1).join('/'));
    } else {
        $rootScope.exitApp();
    }
}.bind(this);

$rootScope.exitApp = function() {
    $rootScope.$emit('showPopover', {
        message: $rootScope.LABELS.QUIT_APP,
        confirmCallback: function() {
            if (typeof navigator.app !== 'undefined') {
                navigator.app.exitApp();
            }
        },
        cancelCallback: doNothing
    });
};

It's working in android and iOS, but not in Windows 10 app.

In W10, navigator.app is undefined.
I read that i'm supposed to suspend app and not exit it, so i tried this windows quirkswritten in cordova doc (https://cordova.apache.org/docs/en/latest/cordova/events/events.html#backbutton) :

$rootScope.exitApp = function() {
    $rootScope.$emit('showPopover', {
        message: $rootScope.LABELS.QUIT_APP,
        confirmCallback: function() {
            if (typeof navigator.app !== 'undefined') {
                navigator.app.exitApp();
            }
            else if (platformUtils.isWindows()) {
                throw new Error('Exit'); // This will suspend the app
            }
        },
        cancelCallback: doNothing
    });
};

throw new Error('Exit') is called and display error in logs, but app is not suspended.

Maybe because i'm in a angular app?

Does any one has an idea?

Upvotes: 0

Views: 559

Answers (2)

P. Stresow
P. Stresow

Reputation: 308

You could also use this:

if (device.platform.toLowerCase() === "windows") {
    //this closes the app and leaves a message in the windows system eventlog if error object is provided
    MSApp.terminateApp();
} else {
    navigator.app.exitApp()
}

Upvotes: 1

Geoffrey Lalloué
Geoffrey Lalloué

Reputation: 1464

Problem is due to $emit used to display popover.
If i throw error on popover callback, this error is not returned to backbutton event callback.

But, on windows platform, we are not supposed to display popover to ask if user is sure to want to leave app, so if I add it to "back" function, it's working.

Final code used to Windows 10 app is :

document.addEventListener('backbutton', function() {
    $rootScope.back();
    $rootScope.$apply();
}, false);

$rootScope.back = function(execute) {
    var path = $location.$$path;
    if (path !== '/') {
        $location.path(path.split('/').slice(0, -1).join('/'));
    } else {
        if (window.device.platform.toLowerCase() === 'windows') {
            throw new Error('Exit'); // This will suspend the app
        } else {
            $rootScope.exitApp();
        }
    }
}.bind(this);

Upvotes: 0

Related Questions