Bug: no applicationDidEnterBackground / applicationWillEnterBackground on Android

Both AppDelegate::applicationDidEnterBackground and applicationWillEnterForeground never get called on Android, so it’s not possible to (for example) pause the game and show a temporary screen when someone calls the user on his device.

The current code in org.cocos2dx.lib does not call these methods @ Activity.onPause and Activity.onResume. The C2DX/Java library code also seems to be somewhat out of sync between the HelloWorld and Tests samples. They’re both different.

I tried to fix it with this (new) code @ cocos2dx/platform/android/CocosdJni.cpp:

void Java_org_cocos2dx_lib_Cocos2dxActivity_nativeOnResume(JNIEnv* env)
{
    if (!cocos2d::CCDirector::sharedDirector()->getOpenGLView()) return;
    cocos2d::CCApplication::sharedApplication().applicationWillEnterForeground();
}

void Java_org_cocos2dx_lib_Cocos2dxActivity_nativeOnPause(JNIEnv* env)
{
    if (!cocos2d::CCDirector::sharedDirector()->getOpenGLView()) return;
    cocos2d::CCApplication::sharedApplication().applicationDidEnterBackground();
}

…where a call to nativeOnResume was added to org.cocos2dx.lib.Cocos2dxActivity.onResume and the same for the nativeOnPause method @ Activity.onPause .

The check for an OGL view was added because the shared application instance doesn’t exist yet when Activity.onResume is first called. But sadly, that won’t work either because libEGL will still complain with the following message: “call to OpenGL ES API with no current context (logged once per thread)”.

Does anyone else have an idea on how to fix this? It’s a very important feature to play nice with phone calls and all that…

Yes, you are right. We will fix the bug as soon as possible.
I have created an issue: #462

Thanks :slight_smile:

But sadly, that won’t work either because libEGL will still complain with the following message: “call to OpenGL ES API with no current context (logged once per thread)”.

Could you please describe it more detail?
I mean when and how the message happened?

Did you call OpenGl ES API in applicationDidEnterBackground / applicationWillEnterBackground?

I have committed the code, you can refer:

I don’t know if I have resolved your problem because I don’t know how the problem generated.
But from the description, you should pause/resume in GL render thread. GL render is runned in
its own thread called renderring thread.

No, I didn’t call any GLES API functions in those methods. I’ll check out the changes and let you know if it works in a few hours. Thanks!

Awesome, it does work! You were right about the rendering thread. Oh, silly me, I had completely forgotten about that!

Thank you!