0

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 *">
Nuru Salihu
  • 4,756
  • 17
  • 65
  • 116
  • backbutton event doesnt support ios – rejo Jun 29 '16 at 05:42
  • @Rejs thanks for the prompt reply. But it doesn't seem to fire on android as well. Btw is there any alternative for the ios ? – Nuru Salihu Jun 29 '16 at 05:45
  • You have to create custom button for back in ios – rejo Jun 29 '16 at 05:46
  • 1
    IOS devices does not have a hardware back button like Android devices. They have a home button. Besides that, at what point `app.initialize()` is executing, have you wrap it within any event ? – Rohit416 Jun 29 '16 at 05:49
  • @Rejs any idea why it does not fire for android from in code above ? – Nuru Salihu Jun 29 '16 at 05:49
  • @Rejs no i didnt. I just called the index.js file after my cordova.js . Please see my update above. – Nuru Salihu Jun 29 '16 at 05:53
  • 1
    document.addEventListener("deviceready", onDeviceReady(), false); change device ready like this. It will register all listeners. – rejo Jun 29 '16 at 05:53
  • @Rejs tried , it does not seem to fire at all. :( – Nuru Salihu Jun 29 '16 at 06:17
  • which version cordova using? – rejo Jun 29 '16 at 06:19
  • 1
    function onBackKeyDown(event) { // Handle the back button event.preventDefault(); alert('alert'); } – rejo Jun 29 '16 at 06:24
  • @Rejs sadly it doesn't fire. I am running `cordova version 6.2.0` – Nuru Salihu Jun 29 '16 at 06:38
  • I tried this, its works for me , It is the latest doc for 6.2 version .. https://cordova.apache.org/docs/de/latest/cordova/events/events.backbutton.html – rejo Jun 29 '16 at 06:43
  • @Rejs i am wondering could it have something to do with my content security policy which i added on the same home.html ? Please see my update. – Nuru Salihu Jun 29 '16 at 06:44
  • http://stackoverflow.com/questions/37784742/override-android-backbutton-behavior-only-works-on-the-first-page-with-phonegap – rejo Jun 29 '16 at 06:55
  • If you remove content security meta is it working? – Shanmugapriya D Jun 29 '16 at 07:59
  • @Rejs thank you for your time. I really appreciate the help. "You have to create custom button for back in ios". Do you mean like add another button on the page ? – Nuru Salihu Jun 29 '16 at 07:59
  • @Rejs, Shanmugapriya. I just release the problem after disabling the whole app. It's a hybrid app and It turns out the user is taking to an external links for authentication and after authentication regardless of the device. Cordova is not referenced on the external device hence the failure. It works when i disable the app.js and try to run within the local js. The app is quite big and a hybrid . Thank you guys. I would try referencing cordova on the external and see. – Nuru Salihu Jun 29 '16 at 08:04

0 Answers0