Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which event to catch for application shut down?

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);
like image 308
xueru Avatar asked Jan 13 '11 06:01

xueru


People also ask

How do I find out what app is trying to shut down?

In need be, you can open Event Logs > Windows Logs > Application > Application events. Now under the General tab, look for The following application attempted to veto the shutdown. You will see the apps which stopped the shutdown.

How do I see shutdown in Event Viewer?

1) View Shutdown and Restart Log from Event Viewermsc” (no quotes) and hit Enter. The Event Viewer windows will open. After that, navigate to Windows Logs > System on the left pane. Click on Filter Current Log on the right.

What does it mean when an app is preventing shutdown?

The main cause why this particular error message (“This app is preventing shutdown” appears) when applications with unsaved data are still opened inside your operating system.


1 Answers

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);
like image 121
fil maj Avatar answered Oct 16 '22 14:10

fil maj