应用程序关闭哪个事件?

xue*_*eru 10 jquery-mobile cordova

我正在使用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);
Run Code Online (Sandbox Code Playgroud)

fil*_*maj 7

我不是100%肯定使用卸载事件,因为在加载不同的网页时,技术上可能(不确定这是否发生在PhoneGap中)也会被触发,即在您的PhoneGap应用程序中将index.html转移到about.html.

至少对于Android,您可以访问简历暂停事件.所以你可以这样做:

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);
Run Code Online (Sandbox Code Playgroud)