Detect if application has been closed or paused
-
20-12-2019 - |
Question
I'm developing my first FirefoxOS app and I need it to backup some data when the user closes or pauses the app. How can I detect this?
I've tried "window.onunload" but it doesn't seem to work.
Thank you.
Solution
You should be able to use page visibility API for background switch.
document.addEventListener("visibilitychange", function () {
if (document.hidden) {
console.log("App is hidden");
} else {
console.log("App has focus");
} });
OTHER TIPS
Event launch visible only if app lost focus.
document.addEventListener('visibilitychange', function() {
if (document['visibilityState'] == "hidden") {
//code if is user out app, no close app
} else if(document['visibilityState'] == 'visible') {
//code if user return on app
}
}, false);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow