繁体   English   中英

如何使用Cordova处理混合移动应用程序的后退按钮功能

[英]How to handle the back button functionality for Hybrid mobile application using cordova

我正在使用cordova创建混合移动应用程序,这是我的新手。

如果用户在首页中两次单击后退按钮,但我无法处理后退按钮功能,在这里我想退出android应用程序。

谁能告诉我如何处理特定页面的后退按钮功能

这是我的index.html

<!DOCTYPE html>

<html>
    <head>

        <meta name="format-detection" content="telephone=no">
        <meta name="msapplication-tap-highlight" content="no">
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
        <link rel="stylesheet" type="text/css" href="css/index.css">
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
        <title>Hello World</title>
    </head>
    <body>
    <div id="main" data-role="page">
        <div class="app">
            <h1>Apache Cordova</h1>
            <div id="deviceready" class="blink">
                <p class="event listening">Connecting to Device</p>
                <p class="event received">Device is Ready</p>
            </div>
        </div>
        </div>
        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
    </body>
</html>

这是我的Index.js:

var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },

    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
            document.addEventListener("backbutton", function() {
                if ( $('.ui-page-active').attr('id') == 'main') {
                    exitAppPopup();
                } else {
                    history.back();             
                }
            }, false);
    },
          onDeviceReady: function() {
        app.receivedEvent('deviceready');
    },
    // Update DOM on a Received Event
    receivedEvent: function(id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');

        console.log('Received Event: ' + id);
    },
        function exitAppPopup() {
        navigator.notification.confirm(
              'Exit PhoneGap ' + device.cordova + ' Demo?'
            , function(button) {
                  if (button == 2) {
                      navigator.app.exitApp();
                  } 
              }
            , 'Exit'
            , 'No,Yes'
        );  
        return false;
    }
};

app.initialize();
<script type="text/javascript" src="cordova.js"></script>
<script>
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() 
{
if (navigator.userAgent.match("Android"))
{
    document.addEventListener("backbutton", onBackKeyDown, true);
}   
}
function onBackKeyDown(e) 
{
if (window.location.hash === "#You page name") 
{
    navigator.app.exitApp();
}
 else 
{
    window.history.back();
}


}

尝试这个 :

document.addEventListener("backbutton", function(e){
if($.mobile.activePage.is('#homepage')){

    navigator.app.exitApp();
}
else {
    navigator.app.backHistory()
}
}, false);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM