Detect app focus lost & gained in javascript?

AppDelegate.cpp has the functions void AppDelegate::applicationDidEnterBackground() and void AppDelegate::applicationWillEnterForeground() for when the app goes into the background and comes into the foreground, respectively. They seem to send out custom events “game_on_hide” and “game_on_show”.

I am unfamiliar with custom events but I managed to get something that seems to work.

When the app hides:
var lstnr = cc.EventListenerCustom.create("game_on_hide", this.gameHidingFunc); cc.eventManager.addListener(lstnr, this);

When the app shows:
var lstnr = cc.EventListenerCustom.create("game_on_show", this.gameShowingFunc); cc.eventManager.addListener(lstnr, this);

So, when the app is hidden or shown, the functions - which I have called gameHidingFunc and gameShowingFunc - will be called. Works for me, anyway.