'beforeunload' event triggers when page is left in the background on Android
Chrome 68 (July 2018) introduced a new Page Lifecycle API: the state diagram supplied in this link does not show the System (browser) calling beforeunload
before putting the page into the frozen
state, but that is clearly what is happening.
Helpfully, the API introduced two new events that trigger as the page enters and exits this state: freeze
and resume
.
I've solved this problem on both mobile chrome and webview displays simply by adding this:
document.addEventListener('resume', (event) => {
// The page has been unfrozen: remove the spinner if it's active
topSpinner.classList.add("closed");
});