Clicking back button after clicking AdMob ad closes app

Clicking back button after clicking AdMob ad closes app


Hi guys,

I have odd behaviour in my game using adMob and the back button.

I catch my back button with a key listener:

auto pKeyListener = cocos2d::EventListenerKeyboard::create();
if (pKeyListener)
    pKeyListener->onKeyPressed  = CC_CALLBACK_2(GameLayer::onKeyPressed, this);
    pKeyListener->onKeyReleased = CC_CALLBACK_2(GameLayer::onKeyReleased, this);
    pEventDispatcher->addEventListenerWithSceneGraphPriority(pKeyListener, this);

And this works fine, until the player clicks the adMob banner (ad shows in browser for example) and clicks back (or use menu to re-activate the game).

When the user now clicks the back button, my game just closes, without any key event being captured on my end. This is really dangerous, as data isn’t saved.
It’s almost like the game crashes, because nothing gets logged after this.

Has anyone seen similar behaviour, or if you have adMob and back button handling implemented, could you check to see if your back button behaviour is the same after you click an ad and go back to your game?

Kind regards,


After some debugging, I found out that this is because the admob webview has focus, and therefor is handling the key events, and not my cocos activity.

The way to fix it, is to ensure the right activity has focus when your game is resumed.
You can do this in the onResume function of your activity:

if (this.getCurrentFocus() != Cocos2dxGLSurfaceView.getInstance())

Hope this helps anyone who runs into the same issue…

Happy coding!

[Cocos3.0 Tutorial] Integrate Admob ( google play services ) with Cocos2d-x v3.0 Final
Clicking back button inside webview closes the app

Thank you very much!:grin:


Can you help me to use this code in new cocos version. I am not finding the requestFocus method.