PhoneGap - android exit on backbutton

前端 未结 5 1779
花落未央
花落未央 2020-12-07 14:08

I am trying to program RSS reader using jquery mobile and cordova. My RSS reader consists of 3 pages (in same HTML document: page1, page2, page3). I am trying to override (h

5条回答
  •  天涯浪人
    2020-12-07 14:30

    To disable the default behavior of the back button on android devices simply register an event handler for the back button. This would prevent the back button from closing the application.

    Code shown below is specifically for Framework7

    $(document).on('page:beforeinit', function (e) {
    if( $.fn.hyellaIMenu.mainView.history && $.fn.hyellaIMenu.mainView.history.length > 1 ){
        document.addEventListener( "backbutton", disableBackButton, false );
    }
    });
    
    function disableBackButton( e ){
        if( $.fn.hyellaIMenu.mainView.history && $.fn.hyellaIMenu.mainView.history.length < 3 ){
            document.removeEventListener("backbutton", disableBackButton );
        }
    
    if( $.fn.hyellaIMenu.mainView.history && $.fn.hyellaIMenu.mainView.history.length > 1 ){
        $.fn.hyellaIMenu.mainView.router.back();
    }
    };
    

    To override the default back-button behavior, register an event listener for the backbutton event.

    NOTE: It is no longer necessary to call any other method to override the back-button behavior.

    https://cordova.apache.org/docs/en/latest/cordova/events/events.html#backbutton

提交回复
热议问题