2011-01-13 83 views
10

我正在使用jQueryMobile和phoneGap作爲跨設備的移動應用程序。我使用html 5本地存儲來保存用戶處理的記錄。要關閉應用程序的哪個事件?

我不知道在應用程序關閉前要捕獲哪個phoneGap事件,因此我可以確保在關閉完成之前保存數據。

基於naughtur的建議,我嘗試了卸載和beforeunload事件,他們都沒有在應用程序關閉期間被解僱。以下是我的代碼剪斷:

function persistTasks(){ 
    alert ("is unloading the app"); 
    offlineTasklist.set("tasks", tasklist); 
} 

function init() { 
    document.addEventListener("unload", persistTasks, false); 
    login(); 
} 

$(document).ready(init); 
+0

這很有趣。嘗試卸載並在卸載事件之前回發它們是否工作。如果沒有 - 我們將不得不在電話apis – naugtur 2011-01-13 07:50:44

回答

7

我不是100%肯定有關使用卸載事件,因爲這些技術可以(不知道如果這發生在PhoneGap的)加載不同的網頁時,即會也被解僱您的PhoneGap應用程序中的index.html到about.html。

至少對於Android,您有權訪問resumepause事件。所以,你可以這樣做:

document.addEventListener('pause', function() { alert('this alert will show up in the background while your app is paused.'); }, false); 
document.addEventListener('resume', function() { alert('this alert will show up when you open the app back up.'); }, false);