Reputation: 765
I am using jQueryMobile and phoneGap for a cross-device mobile application. I use html 5 local storage to persist records that worked on by user.
I don't know which phoneGap event to catch just before application shut-down so I can make sure data is saved before shutdown completes.
Base on suggestion from naughtur, I tried both unload and beforeunload events, neither of them got fired during app shutdown. Following is my code snip:
function persistTasks(){
alert ("is unloading the app");
offlineTasklist.set("tasks", tasklist);
}
function init() {
document.addEventListener("unload", persistTasks, false);
login();
}
$(document).ready(init);
Upvotes: 10
Views: 7273
Reputation: 2312
I'm not 100% sure about using unload events, because those technically could (not sure if this happens in PhoneGap) also be fired when loading a different web page, i.e. going index.html to about.html inside your PhoneGap app.
At least for Android, you have access to resume and pause events. So you can do:
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);
Upvotes: 7
Reputation: 16915
The unload
event is already avaliable in Android and Blackberry and will be implemented across all platforms soon.
See comments here: http://phonegap.lighthouseapp.com/projects/20118-android/tickets/67-missing-event-for-application-shut-down
Upvotes: 1