PhoneGap - android exit on backbutton

前端 未结 5 1760
花落未央
花落未央 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:16

    You need to wait for the device to be ready to add the event listener:

    document.addEventListener("deviceready", onDeviceReady, false);
    
    function onDeviceReady(){
        document.addEventListener("backbutton", function(e){
           if($.mobile.activePage.is('#homepage')){
               e.preventDefault();
               navigator.app.exitApp();
           }
           else {
               navigator.app.backHistory();
           }
        }, false);
    }
    
    0 讨论(0)
  • 2020-12-07 14:22
    function onLoad() {
            document.addEventListener("deviceready", onDeviceReady, false);
        }
    
        //enter code here enter code heredevice APIs are available
        //enter code here
        function onDeviceReady() {
            // Register the event listener
            document.addEventListener("backbutton", onBackKeyDown, false);
        }
    
        // Handle the back button
        //
        function onBackKeyDown() {
        }
    
    0 讨论(0)
  • 2020-12-07 14:23

    If you don't want to use Jquery Mobile, change $.mobile.activePage.is('#homepage') to document.getElementById('#homepage') on @mornaner answer, as on following code:

    document.addEventListener("deviceready", onDeviceReady, false);
    
    function onDeviceReady(){
        document.addEventListener("backbutton", function(e){
           if(document.getElementById('#homepage')){
               e.preventDefault();
               navigator.app.exitApp();
           }
           else {
               navigator.app.backHistory()
           }
        }, false);
    }
    

    Through this way, don't need to download Jquery Mobile gibberish only for this purpose. Also, activePage is deprecated as of JQuery mobile 1.4.0 and will be removed from 1.5.0. (Use the getActivePage() method from the pagecontainer widget instead)

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-07 14:34

    If you don't want to use any library, you can use window.location.hash to get the "panel" your app is on. Example :

    function onDeviceReady(){
        document.addEventListener("backbutton", function(e){
            if(window.location.hash=='#home'){
                e.preventDefault();
                navigator.app.exitApp();
            } else {
                navigator.app.backHistory()
            }
        }, false);
    }
    document.addEventListener("deviceready", onDeviceReady, false);
    
    0 讨论(0)
提交回复
热议问题