Is there any Cordova/DOM event that I can hook into on iOS when the user directly closes the app (by double-tapping the Home button and then swiping the app away)? The pause event does successfully fire when the Home button is pressed once and the app is sent to the background, but the double-tap+close seems not to do this -- at least in the emulator.
I am using the pause event to capture and store app state, so closing without saving will leave the user with no previous state to return to, or worse, an old state.
I am aware of the "iOS Quirks" warning in the documentation that says:
In the pause handler, any calls to the Cordova API or to native plugins that go through Objective-C do not work, along with any interactive calls, such as alerts or console.log(). They are only processed when the app resumes, on the next run loop.
...but unless someone corrects me, I don't think this is the issue here
This question has been asked a lot on ionic forum and the consensus at the moment appears to be that this is not possible. I had this same desire for a time keeping app. I ended up deciding to use setInterval at a frequency acceptable (for me 3 seconds was fine). Agree this should be a feature
Related
I have a web app where a parent page displaying a list of records opens up a new tab ('child') to edit a clicked-on record. I want to track who has a page open, so I can display a message if more than one person is editing a unique record. This means reporting when a page is closed. I have assigned each page a GUID to facilitate recognition of the page instance.
So javascript in the browser needs to detect several scenarios:
browser tab closed
browser refresh
browser navigation to hyperlink
browser navigation forward/back
At the moment, all of these appear to trigger the window.onbeforeunload event. However I use this event to warn of changes in the underlying data, which means the event returns the confirmation text, and there is no way of knowing in this event if the user subsequently confirms or cancels the page unload. So I can't use this event to track page closure.
According to a number of sources the window.onunload event should be triggered in all of the above scenarios (and if it was, I could use it), but testing under Chrome on Windows is only triggering this event in scenario 1 (when the tab is closed). It works fine for that.
I'm pretty surprised by the lack of information around this - surely it's a bread and butter requirement in modern sites?
Has window.onunload been deprecated lately in some scenarios, or in some scenarios in some browsers? Without a reliable hook that takes place when the page is about to be replaced with some other information, it's impossible to monitor closing of a page. Any other workarounds?
I know that the two unload events suppress blocking functions (such as alerts) in the handler. However they appear to do hit breakpoints, do a console.log and allow Ajax calls just fine. I'm pretty sure they are not being fired in events 2,3 and 4 - it's not just that my debugging is being blocked.
While there appear to be answers on SO already (most of which don't work or are deprecated), I posted this because browser events are a shifting-sands scenario as security issues evolve, so I wanted to find out where we are in 2021.
Actually, it looks as if this might be the solution: https://developers.google.com/web/updates/2018/07/page-lifecycle-api#the-unload-event
Google discourages use of the unload event because it is (a) unreliable on mobiles and (b) blocks the caching of pages. It is also advised to only add the beforeunload event just before it is used, and to remove it afterwards, because it also blocks the caching of pages (I note that this is not really practical for me, however, as I use it to guard against unintentional closing of a page after a possibly significant amount of data has been entered, and this could happen at any moment).
So as of July 2018, and still best practice as of July 2021, this would be the recommended way to detect the unloading of a page:
const terminationEvent = 'onpagehide' in self ? 'pagehide' : 'unload';
addEventListener(terminationEvent, function(event) {
// handler code here ...
}, { capture: true });
This has been tested in a small ASP NET Core project using an AJAX callback to report the page termination, and appears to work reliably in Chrome and Edge. Also works in IE11 as long as
<meta http-equiv="x-ua-compatible" content="IE=edge">
is present.
Is there any Out Of the Box Vaadin 10 (and higher) event similar to window.onbeforeunload in JavaScript?
I've tried to use onDetach() or beforeLeave(), but it only works inside UI, and when user reloads the page or closes the page it's not working.
You can use the approach described in https://vaadin.com/forum/thread/17523194/unsaved-changes-detect-page-exit-or-reload that was already suggested in a comment.
At the same time, I'd urge you to be really careful with beforeunload events since they are in some situations fired even though the user is actually not navigating away from the page.
The most common case is if the user clicks a link that starts a download. In that case the browser will fire the event immediately when the user clicks the link. Slightly later when the browser receives the response headers, it will discover that it's a download and not a new HTML page to display. The end result is then that beforeunload has been fired but the previous page is still kept running.
If you want to use the event for cleanup, then the best approach today is probably a combination of the unload event and then using the new-ish Beacon API for notifying the server that the user has actually navigated away. Integrating this into a Vaadin application will require slightly more JavaScript, but it has the benefit that it will actually work.
I am porting a html5 game to win8. To save the game state, I call a function save_game (which uses localStorage to store some data) in window.unload, which of course does not work here. So I use WinJS.Application.oncheckpoint instead. Strangely, if I launch a game and press alt-f4, the game state is not saved. Debugging in VS with console.log in the event handler, I found that it seems to be triggered only when I resume the app. Bizarrely, if I put a break point in the code, the event handler will then be correctly executed during suspension.
Anyone has any idea why this happen? Is this a bug in win8?
Thanks in advance.
You are seeing known behavior when debugging your app with VS. When you close an app via user action (Alt-F4 / top swipe), the app is actually held open for a while by the debugger. If you set a breakpoint in oncheckpoint, debug app, press Alt-F4 and then WAIT for ~5 seconds or so, you will hit the breakpoint. The best way to simulate a user "close" event is to use the Suspend and Shutdown option from within VS that will immediately fire oncheckpoint.
Win8 Metro provides callback for when the process enters the activated and suspended states but what about when the app comes into the foreground or goes into the background? The process isn't necessarily suspended as soon as the app goes into the background and similarly it may come into the foreground while being still activated from the previous launch.
Are there any events in Javascript that can be used to execute code on foregrounding or backgrounding the app?
When the application is placed into the background, but not suspended (yet), you can use the "visibilitychanged" event, and "hidden" property on the document object:
http://msdn.microsoft.com/en-us/library/ie/hh773167(v=vs.85).aspx
These are fired the moment your app is no longer visible (e.g. the user hits the start button, or swipes to another application.
If your app is then suspended, you will also get the appropriate suspend events then.
From my understanding, as soon as a metro app completely looses focus (goes completely off screen) it is given 15 seconds (I think) by the OS to run its OnSuspend code, before being stopped and suspended. This happens every time the whole app loses complete focus and the reverse happens when it gets focus again (the user switches back to it).
The code that you would to run in these foreground and background events should be put in the Suspension and Reactivation event handlers
In Metro-style apps, the application will suspend when the user switches away from it and resume when the user switches back to it. Metro-style apps run full-screen and immersive, so "suspend" is equivalent to what you are thinking of as "going into the background" and resuming is equivalent to coming into the foreground.
Once the suspend event is fired (when a user swipes away from your app), your app has 5 seconds to store off state. In the suspend mode, your main thread of execution is suspended but the app is still held in memory. The user may come back to your app shortly, and then the thread is resumed and that state that you stored is never used. But, at some point, Windows may have too much in memory and need to terminate a process. If your app is terminated, then you can use the state that you stored to restore your app to the way it was when it was terminated on the next time that it is run.
Check out these resources for more info:
* The different Application Execution states: http://msdn.microsoft.com/en-us/library/windows/apps/windows.applicationmodel.activation.applicationexecutionstate.aspx
* Guidelines around this: http://msdn.microsoft.com/en-us/library/windows/apps/hh465088.aspx
I am developing a web-app in iOS using PhoneGap. I need to make a javascript function call when the application goes into the background.
I am calling the function from "applicationWillResignActivity" method, but its not executing till the app comes to the foreground next time. Any other non-javascript code is getting executed. This happens only when I press home button. In other cases, like pressing power button when the app is running, and pressing the home button twice, the javascript function gets executed.
Is there some constraint over UIWebView of such kind? Can someone please tell if there is a way to make javascript work when user presses Home button?
It seems there is no way to capture such event. So just to self-answer this question, that there indeed is no delegate method to run JS code when app goes into background.