angularrxjswpa

Angular WPA How to know if the application is in background


I use a SetTimeout to refresh data from a web api every 10s. I stop it (clean) in the onDestroy. Work perfectly 😁

But, as my app is WPA, I realized that, when I hit the Home button of my phone, the app is still making refresh calls. Is there any way (property or event) to know when the app is running in background, so I could stop my timer.

Thx for your time 😎


Solution

  • Finally found something : juste use HostListener to watch window focus/blur events and then enable/disable my timer.

    @HostListener('window:focus', ['$event'])
    onFocus(event: FocusEvent): void {
      this.nasTimer.start();
    }
    
    @HostListener('window:blur', ['$event'])
    onBlur(event: FocusEvent): void {
      this.nasTimer.stop();
    }