2017-02-24 144 views
3

我一直在努力解決這個問題,在我的科爾多瓦應用程序中,無論如何,後退按鈕都會退出應用程序。我嘗試了我在網上遇到的所有解決方案,但都沒有取得任何成功。防止退出按鈕退出科爾多瓦應用程序

我試過的所有解決方案(下面的例子)都產生了相同的結果。

document.addEventListener("backbutton", onBackKeyDown, false); 

function onBackKeyDown() { 
    // Handle the back button 
} 

我的回調中的代碼執行沒有問題,但執行後,它退出應用程序。我可以通過在我的功能ReferenceError防止出口,例如

console.log(undefinedVar); 

但這顯然似乎不是最好的做法。

我試過的其他解決方案包括使用event.preventDefault()從回調函數和離子的registerBackButtonAction函數。

任何建議將不勝感激。

+0

此代碼對我的作品 - document.removeEventListener( 「後退按鈕」,onBackButton,FALSE);函數onBackButton(E){ e.preventDefault(); navigator.notification.confirm(「你確定要退出嗎?」,onConfirm,「確認」,「是,否」); } function onConfirm(button){ if(button == 2){ return; } else { navigator.app.exitApp(); } } – Gandhi

+0

ü可以嘗試我的代碼我試過了,它爲我工作..它有助於防止回到其他ba上一頁,...禁用Android的後退按鈕鍵Cordova以及電話差距....謝謝 –

回答

0
enter code here 

<!DOCTYPE html> 
 
<html> 
 
    <head> 
 
    <title>Stopping 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('Back Button Disabled'); 
 
     console.log('Back Button Disabled'); 
 
    } 
 
    </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>

+0

ü可以嘗試這一個我試過了,它爲我工作..謝謝 –

相關問題