我正在将 jQueryMobile 和phoneGap 用于跨设备移动应用程序。我使用 html 5 本地存储来保存用户处理的记录。
我不知道在应用程序关闭之前要捕获哪个phoneGap 事件,因此我可以确保在关闭完成之前保存数据。
根据naughtur的建议,我尝试了unload和beforeunload事件,它们都没有在应用程序关闭期间被解雇。以下是我的代码片段:
function persistTasks(){
alert ("is unloading the app");
offlineTasklist.set("tasks", tasklist);
}
function init() {
document.addEventListener("unload", persistTasks, false);
login();
}
$(document).ready(init);
最佳答案
我不是 100% 确定是否使用卸载事件,因为从技术上讲,这些事件(不确定这是否发生在 PhoneGap 中)在加载不同的网页时也会被触发,即在 PhoneGap 中将 index.html 转到 about.html应用程序。
至少对于 Android,您可以访问 resume和 pause事件。所以你可以这样做:
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);
关于cordova - 应用程序关闭时要捕获哪个事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4677507/