Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to control android backbutton routes?

Tags:

onsen-ui

The default in Onsen is that the app closes/exits when the device backbutton is pressed. Is there any way to control that in Onsen to also mimic the ons-navigator action/page history?

Thanks!

like image 443
SOF Avatar asked Jun 04 '14 15:06

SOF


1 Answers

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();"
    }
}
like image 162
KNaito Avatar answered Nov 10 '22 11:11

KNaito