Where/how do you listen to appStateChange?


#1

I’m trying to catch an event when the app moves back to the foreground. I have the following in the AppComponent from the ionic v4 sidemenu template. But it never seems to catch the event. suggestions?

@Component({
  selector: 'app-root',
  templateUrl: 'app.component.html'
})
export class AppComponent {
  public appPages = [
    {
      title: 'List',
      url: '/list',
      icon: 'list'
    },
  ];

  constructor(
    private platform: Platform,
    private splashScreen: SplashScreen,
    private statusBar: StatusBar,
  ) {
    this.initializeApp();
  }

  initializeApp() {
    this.platform.ready().then(() => {
      this.statusBar.styleDefault();
      this.splashScreen.hide();
      this.listenAppState();
    });
  }

  async listenAppState(){
    const device = await Device.getInfo();
    switch (device.platform){
      case 'ios':
      case 'android':
        App.addListener('appStateChange', (state: AppState) => {
          // state.isActive contains the active state
          console.log('"&&& App state changed. Is active?', state.isActive);
        });
        break;
    } 
  }
  
}

#2

Nevermind. it works as written. There was an exception in console statement I was using JSON.stringify(item).

But I would like suggestions on how to catch these events in the Safari Debugger.