Why is Android WebView sending a 'beforeunload' event when it isn't unloading?

Viewed 561

My app, which is Cordova based, listens for 'beforeunload' in order to perform some cleanup and to put the app into an exiting state, whereby it will not attempt certain things that are likely to not complete (e.g. database updates).

The problem is that recently (4 customers have reported issues related to this over the past month, for an app that has not changed in 12 months, has been working fine for many years) my app is being sent 'beforeunload' event after being in background for say 5 mins, but it then doesn't actually unload.

So the app is left running, but in a state where it thinks it is exiting, which is causing all sorts of issues.

I have a couple of couple of fixes ready to ship. One is to ignore the unload event, which as the app generally stays running in background these days, not entering an exiting state is probably not a big deal.

Another is to switch to use 'unload' event rather than 'beforeunload' which I have yet to test out properly but may be a more appropriate option.

My question is really, is this odd behaviour of 'beforeunload' to be expected/known or is it a bug in webview?

My use of beforeunload goes back 4 or 5 years, so it may well be outdated and need a review.

1 Answers

Maybe use cordova pause and resume events to prevent beforeunload logic execution when app is in the background.

Related