How do I disable Android Back button on one page and change to exit button on every other page

前端 未结 4 943
北海茫月
北海茫月 2020-12-16 15:37

I am developing an Android application using Phonegap that interacts with my Drupal site. I have re-assigned the Android \"Back\" button to prompt the user to log off from t

相关标签:
4条回答
  • 2020-12-16 15:58
    document.addEventListener("deviceready", onDeviceReady, false);
    function onDeviceReady() {
        document.addEventListener("backbutton", function (e) {
            e.preventDefault();
        }, false );
    }
    
    0 讨论(0)
  • 2020-12-16 15:58

    You have to override back button action in device ready... here is a full working example "index.html"

    <!DOCTYPE html>
    <html>
      <head>
        <title>Back Button</title>
        <link rel="stylesheet" type="tex`enter code here`t/css" href="style.css">
        <script>
            function getTitle() {
                document.getElementById("ct").innerHTML = "DEMO: " + document.title;
            }
        </script>
        <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
        <script type="text/javascript" charset="utf-8">
        // Wait for device API libraries to load
        //
        function onLoad() {
            document.addEventListener("deviceready", onDeviceReady, false);
            getTitle();
        }
        // device APIs are available
        //
        function onDeviceReady() {
            // Register the event listener
            document.addEventListener("backbutton", onBackKeyDown, false);
        }
        // Handle the back button
        //
        function onBackKeyDown() {
            alert('Backbutton key pressed');
            console.log('Backbutton key pressed');
        }
        </script>
      </head>
      <body onload="onLoad()">
      <ul id="nav">
            <li><a href="index.html">&larr; Back</a></li>
            <li><a href="#" id="ct" onclick="location.reload();"></a></li>
        </ul>
      </body>
    </html>
    

    I used this code and back button is overridden finally....

    Ref--> https://github.com/amirudin/PhoneGapPluginDemo/blob/master/www/evt-backbutton.html

    0 讨论(0)
  • 2020-12-16 16:01

    deviceready is important. If not used, sometimes you can get blocking Back button, sometimes not. Often in debug it works, in production no.

    document.addEventListener("deviceready", onDeviceReady, false);
        function onDeviceReady() {
            document.addEventListener("backbutton", function (e) {
                e.preventDefault();
            }, false );
    }
    

    EDIT 2013-11-03 Common mistake is that development is performed on desktop and cordova script is excluded. One then forgets to include the cordova script for the mobile version.

    0 讨论(0)
  • 2020-12-16 16:06

    Try this:

    document.addEventListener("backbutton", function(e){
        if($.mobile.activePage.is('#login_page')){
            e.preventDefault();
        }
        else {
            if (confirm("Are you sure you want to logout?")) {
                /* Here is where my AJAX code for logging off goes */
            }
            else {
                return false;
            }
        }
    }, false);
    
    0 讨论(0)
提交回复
热议问题