Cocos Creator Background Events: EVENT_HIDE, EVENT_SHOW and Cleanup

Register and Remove App Lifecycle Listeners

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import { game, Game } from 'cc';

export class AppLifecycle {
  static onLoad() {
    game.on(Game.EVENT_HIDE, this.onHide, this);
    game.on(Game.EVENT_SHOW, this.onShow, this);
  }

  static onHide() { console.log('Application entered the background'); }
  static onShow() { console.log('Application returned to the foreground'); }

  static onDestroy() {
    game.off(Game.EVENT_HIDE, this.onHide, this);
    game.off(Game.EVENT_SHOW, this.onShow, this);
  }
}

Call onLoad during startup and onDestroy when the listener owner is released.