Close or restart/reload a PWA that is in the background


I have a web app which I really need to close when it’s no longer in use, but iOS devices don’t close apps (they simply push them to the background) and the home button on Android does the same thing, so when the user ‘loads’ them again they simply carry on from where they left off instead of restarting.

I need a way to detect that the app is no longer being used and either make it close or restart/reload.

Anyone know how I can do this?


I think this might have to be under app/program control. I don’t do this but you might try making an “exit” button somewhere that is available from any of your forms. When the user selects this, show a new form that has another button like “Return to app” (maybe with a label saying “App is sleeping, select Return to App to wake up.”). The button on_click event executes window.location.reload().

The app may still be running in the background (not sure about this depending on the OS) so if you need to do any house cleaning (like stopping any code executing via a timeinterval), do it with the exit button. If you are concerned that someone else could run the app without authentication this should fix that as well. However, if your concern is an unwanted user could see your localstorage variables I would suggest any sensitive data be encrypted.


You could try something like this:

document.addEventListener("pause", onPause, False);
function onPause() {

You can add a function to window.onblur and get your app to the state you want your user to pick it from when it is in the foreground again.

If your app can detect the pause state. Why not do a url reload.


Thanks for all the suggestions.

I’ve since also come across window.document.hasFocus(); as another way of determining if it’s in the foreground, and apparently timers (such as settimeout) are throttled in the background, so setting a continuous fixed duration timer and checking that it’s not taken much longer than it should is another possible method of determining if it’s in the background, so there’s quite a lot to test here.

I was hoping there was a definitive way, as in some method you’re supposed to use which is the proper way and will work on all devices, but I guess I was asking for too much. :smiley: