I am trying to listen to the backbutton click on android devices as explained Here. below is my code
var app = {
initialize: function() {
this.bindEvents();
},
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
document.addEventListener('resume', this.onDeviceResume, false);
},
onDeviceReady: function() {
document.addEventListener('backbutton', this.onBackKeyDown, false);
app.receivedEvent('root');
codePush.sync();
},
onBackKeyDown: function (e) {
if (window.location.pathname === '/') {
navigator.notification.confirm(
'You are the winner!', // message
onConfirm, // callback to invoke with index of button pressed
'Game Over', // title
'Restart,Exit' // buttonLabels
);
e.preventDefault();
navigator.app.exitApp();
} else {
navigator.app.backHistory();
}
}
}
app.initialize();
My event don't seem to fire on backbutton click. I even added an exit to test yet nothing change.
onBackKeyDown: function (e) {
navigator.app.exitApp();
}
Please how do i achieve that? Is there something i m doing wrong ? Any help would be appreciated.
And my home.html below
<body>
<div id="root" class="root app">
<div class="mobile-page">
<img class="spinner" src="img/my-symbol-logo.svg" />
<p class="event listening">Connecting to Device</p>
<p class="event received">Device is Ready</p>
</div>
</div>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="index.js"></script>
Update
content security policy.
<meta http-equiv="Content-Security-Policy" content="default-src * 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">