How to control android backbutton routes?

独自空忆成欢 提交于 2019-12-01 05:51:58

In case of PhoneGap/Cordova, backbutton event is fired when the backbutton is pressed. Therefore, you can set the eventhandler s.t.

document.addEventListener("backbutton", onBackKeyDown, false);

In eventhandler function, you can call popPage method of navigator by obtaining the navigator scope s.t.

function onBackKeyDown() {
    // Handle the back button
    alert("Backbutton is pressed!");
    var element = document.querySelector( ".navigator-container");
    var scope = angular.element( element ).scope();
    scope.popPage();
}

If you are using Monaca, the hybrid application framework based on Cordova, the backbutton event is not fired. Instead that you can use the .ui file in which the Backbutton event is defined s.t.

{
    "event" : {
        "onTapBackButton" : "onBackKeyDown();"
    }
}

No need to handle backbutton event to prevent application being killed. Onsen-ui Added support to android back-button on ons-navigator and ons-sliding-menu from v1.1.1.

You can check here

You can controll it with "disableDeviceBackButtonHandler" after ons.ready event. After that add a event listener for back button and do anything you want.

ons.ready(function() {
  ons.disableDeviceBackButtonHandler();

  // Use Cordova handler
  window.document.addEventListener('backbutton', function() {
    // Handle backbutton event
  }, false);
});

Just check this article: https://onsen.io/guide/overview.html#HandlingBackButton

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!