I am using cocos2d-x3.17.2 for developing android games.
When I am trying to use Admob Rewarded Video in Android. I noticed that applicationWillEnterForeground called twice after I close the rewawded ad after watching it.
First time is onWindowFocusChanged = true, here is the code in cocos2d-x, it will be called right after I click the close button on ad.
@Override
public void onWindowFocusChanged(boolean hasFocus) {
Log.d(TAG, “onWindowFocusChanged() hasFocus=” + hasFocus);
super.onWindowFocusChanged(hasFocus);
this.hasFocus = hasFocus;
resumeIfHasFocus();
}
private void resumeIfHasFocus() {
//It is possible for the app to receive the onWindowsFocusChanged(true) event
//even though it is locked or asleep
boolean readyToPlay = !isDeviceLocked() && !isDeviceAsleep();
if(hasFocus && readyToPlay) {
this.hideVirtualButton();
Cocos2dxHelper.onResume();
mGLSurfaceView.onResume();
}
}
Second time is onResume. this will be called after the ad totally was removed from screen.(there is a move out animation for Ad).
@Override
protected void onResume() {
Log.d(TAG, “onResume()”);
paused = false;
super.onResume();
if(gainAudioFocus)
Cocos2dxAudioFocusManager.registerAudioFocusListener(this);
this.hideVirtualButton();
resumeIfHasFocus();
}
If I give user reward as callback after first applicationWillEnterForeground was called, the game crashes randomly, because the ad close effect takes 0.5 second to move out, it’s rendering foreground.
How can I really know when can I give rewards to user after ad closed.