How to logout user, when android twa app is minimized or sent to background

270 Views Asked by At

I have built an android twa(trusted web activity) app from angular 8 PWA(progressive web app) using bubblewrap. The app has Google Signin button, to login into the app, and view the other screens in the app.

When the app is minimized, i would like to logout the user automatically. This means, when the user brings the app again to foreground, user should see the login screen.

Are there any callback methods available to TWA, for android app lifecycle?

1

There are 1 best solutions below

2
On

In order to track if a web page is visible to the user, you can use the Page Visibility API on the web app. You could, for instance, listen for the visibilitychange event and sign out the user when the page is no longer visible.

document.addEventListener('visibilitychange', function() {
  if (document.visibilityState !== 'visible') {
    signOut()
  } 
});

The advantage of this approach instead of hooking up to the Android lifecycle is that it will work on the standalone browser, including when switching tabs, and installed PWAs.