Detect app focus lost & gained in javascript?

Dear All,

Do we have some way to know that app has lost or gained focus.

For example, while playing the game, if a phone call is received, I want to pause the game.

Thanks in advance…

Also I would like to know it. Anyone knows?

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.

Thanks grimfate.

So, I need to add these event handlers in all Scenes.

I wonder if I can add this only on one place in the code.

Thanks