0

I have a small Phonegap app that has 2 HTML pages (index.html & error.html). When error.html is active I want the back button (hardware button on Android device) to actually go back to index.html instead of exiting the app. I have tried the following (just to trigger an even on the backbutton) with no success:

<script>
    document.addEventListener("deviceready", onDeviceReady, false);

    function onDeviceReady() {
        console.log("PhoneGap is ready");
        document.addEventListener("backbutton", onBackKeyDown, false);
    }

    function onBackKeyDown()
        {
            console.log("yes");
            return false;
        }
</script>

What am I missing here?

Sarantis Tofas
  • 5,097
  • 1
  • 23
  • 36

4 Answers4

4

document.addEventListener("backbutton") will not work for Onsen UI.

It looks that in order to handle the hardware back button with Onsen-UI you should do one of the following:

ons.ready(function() {
// To disable a navigator back button handler
navigator.getDeviceBackButtonHandler().disable();

// Or to change the behavior
navigator.getDeviceBackButtonHandler().setListener(function(event) {
console.log("back button pressed")});
}

Or

<ons-page on-device-backbutton="doSomething()">
Some page content
</ons-page>

Please read more on their documentation https://onsen.io/v1/guide.html

Sarantis Tofas
  • 5,097
  • 1
  • 23
  • 36
  • are u using the `onload` event? – Sarantis Tofas Sep 18 '16 at 18:56
  • @DustinTinsley are you checking the chrome console or the monaca console? cause i remember using monaca and log was not working at all, i had to open the chrome dev console – Sarantis Tofas Sep 18 '16 at 19:07
  • To big to post here. Here is a link to the full code: http://s000.tinyupload.com/?file_id=05896617351842418294 – Dustin Tinsley Sep 18 '16 at 19:14
  • The console works for everything except that so I would think it is working as it should be (console i mean). i think i am just overlooking something in my code. – Dustin Tinsley Sep 18 '16 at 19:17
  • @DustinTinsley updated my answer, you are using the Onser UI framework and the way to listen to the hardware button can be found on their documentation. – Sarantis Tofas Sep 18 '16 at 19:38
  • I am still at a loss. I have tried these methods with no luck.... I think I need a break. I am sure i am just doing something wrong... – Dustin Tinsley Sep 18 '16 at 21:08
  • @DustinTinsley yes the back button with Onsen looks like its a little bit tricky but i am sure that when you have the time and you read the documentation everything is going to be fine :D – Sarantis Tofas Sep 18 '16 at 22:05
  • i would like to think so, but i am so confused with this... ugh. – Dustin Tinsley Sep 18 '16 at 22:59
0

. It is important to understand that this event is fired by Cordova so it requires cordova.js file or similars (loader.js in Monaca) to be included in the app.

http://onsen.io.s3-website-us-east-1.amazonaws.com/v2/guide/cordova.html

below is full code, make sure u have added cordova.js in root folder ie in www folder and linked it to our html page

});

Syed Kalam
  • 11
  • 4
0

This solution (thanks to @alecdwm in this post) work for me in Onsen UI PWA. It prevents to go back in history state using hardware back button while you can use Onsen navigator methods. I think it is useful for Cordova/Phonegap too.

window.addEventListener('load', function () {
    window.history.pushState({ noBackExitsApp: true }, '');
});
window.addEventListener('popstate', function (event) {
    if (event.state && event.state.noBackExitsApp) {
        BackButton();
        window.history.pushState({ noBackExitsApp: true }, '');
    }
});
function BackButton(){
//use navigator.popPage() OR navigator.resetToPage OR .....
}

Ingd.

ingd
  • 119
  • 9
0
var currentPage = null;
ons.ready(function() {
  ons.enableDeviceBackButtonHandler();
  // Set a new handler
  ons.setDefaultDeviceBackButtonListener(function(e) {
    if (currentPage == 'home') {
      if (navigator.app) {
        navigator.app.exitApp();
      } else if (navigator.device) {
        navigator.device.exitApp();
      } else {
        window.close();
      }
    } else {
      $('ons-back-button', '#' + currentPage)
        .parent()
        .trigger('click');
    }
  });

  /**
   * Loads a page
   */
  window.fn.load = function(page, pageData) {
    var content = document.getElementById('content');
    var menu = document.getElementById('menu');
    content
      .load(page, data)
      .then(menu.close.bind(menu))
      .then(function() {
        // Save a reference to current page (because there is no navigator object)
        currentPage = $('ons-page', $('#content')).prop('id');
      });
  };
});

// ons-page example
<ons-page id="other-menu-page">
  <ons-toolbar>
    <div class="left" onclick="fn.load('pages/home.html')" tappable>
      <ons-back-button>Back</ons-back-button>
    </div>
etc...

NOTE: this is when ons-splitter is used instead of ons-navigator

TomoMiha
  • 1,218
  • 1
  • 14
  • 12