Context
I'm trying to simulate a WebView using Intel XDK. Before reading some post on Stack Overflow I read that the better way to archive this is using a window.location = "url";
. It worked like a charm.
Problem
Now I want to bind the hardware backbutton, so the user can confirm is he want or not to close the app. The problem is that it only works if the window.location
doesn't execute.
Code
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<script src="cordova.js"></script>
<script>
window.location = "https://google.com";
var tried = false;
document.addEventListener("backbutton", backButton, false);
function backButton(){
if (tried){
navigator.app.exitApp();
}
else {
alert('TEST: Next time APP should close');
tried = !tried;
}
}
</script>
</head>
<body>
</body>
</html>
Results
Commenting window.location
Executing window.location
Thanks!
Each page has it's own javascript, if you switch the page all the javascript will be lost.
If you want to make it work, you'll need to have the same backbutton handler code on any page where you redirect using location.href, and you'll need to link cordova.js too.
That means you can only make it work on pages you can control the code they load, not on google.
If you want to do it on any page, even on pages you can't control, then you can't use cordova javascript code as it will disappear, you'll need to change how cordova java code controls the back button behaviour, it's done on this method:
You'll have to detect the KeyEvent.KEYCODE_BACK
and show a native dialog to close the app there