Cocos2dxActivity is created twice causing opengl errors

Sometimes, when sending the app to the background and resuming it, Cocos2dxActivity’s onCreate method is called. This causes cocos_android_app_init to be called, and this triggers an assert in CCApplication-android.cpp. ( CCAssert(! sm_pSharedApplication, “”) )
I think this is because it tries to initialize everything again, without having exited the application.
After this happens, I get a bunch of gl_errors and the applications becomes unusable:

04-23 19:38:50.465: W/Adreno-ES20(31595): <gl_draw_error_checks:614>: GL_INVALID_OPERATION
04-23 19:38:50.465: D/cocos2d-x debug info(31595): OpenGL error 0x0502 in /Users/andres/Documents/Projects/MetroMon/frameworks/runtime-src/proj.android/…/…/cocos2d-x/cocos/./renderer/CCTextureAtlas.cpp drawNumberOfQuads 693
04-23 19:38:50.465: W/Adreno-ES20(31595): <gl_draw_error_checks:614>: GL_INVALID_OPERATION
04-23 19:38:50.465: D/cocos2d-x debug info(31595): OpenGL error 0x0502 in /Users/andres/Documents/Projects/MetroMon/frameworks/runtime-src/proj.android/…/…/cocos2d-x/cocos/./renderer/CCTextureAtlas.cpp drawNumberOfQuads 693
04-23 19:38:50.465: W/Adreno-ES20(31595): <gl_draw_error_checks:614>: GL_INVALID_OPERATION
04-23 19:38:50.471: D/cocos2d-x debug info(31595): OpenGL error 0x0502 in /Users/andres/Documents/Projects/MetroMon/frameworks/runtime-src/proj.android/…/…/cocos2d-x/cocos/./renderer/CCTextureAtlas.cpp drawNumberOfQuads 693
04-23 19:38:50.471: W/Adreno-ES20(31595): <gl_draw_error_checks:614>: GL_INVALID_OPERATION
04-23 19:38:50.471: D/cocos2d-x debug info(31595): OpenGL error 0x0502 in /Users/andres/Documents/Projects/MetroMon/frameworks/runtime-src/proj.android/…/…/cocos2d-x/cocos/./renderer/CCTextureAtlas.cpp drawNumberOfQuads 693
04-23 19:38:50.471: W/Adreno-ES20(31595): <gl_draw_error_checks:614>: GL_INVALID_OPERATION
04-23 19:38:50.476: D/cocos2d-x debug info(31595): OpenGL error 0x0502 in /Users/andres/Documents/Projects/MetroMon/frameworks/runtime-src/proj.android/…/…/cocos2d-x/cocos/./renderer/CCTextureAtlas.cpp drawNumberOfQuads 693
04-23 19:38:50.476: W/Adreno-ES20(31595): <gl_draw_error_checks:614>: GL_INVALID_OPERATION
04-23 19:38:50.476: D/cocos2d-x debug info(31595): OpenGL error 0x0502 in /Users/andres/Documents/Projects/MetroMon/frameworks/runtime-src/proj.android/…/…/cocos2d-x/cocos/./renderer/CCTextureAtlas.cpp drawNumberOfQuads 693
04-23 19:38:50.476: W/Adreno-ES20(31595): <gl_draw_error_checks:614>: GL_INVALID_OPERATION
04-23 19:38:50.492: D/cocos2d-x debug info(31595): OpenGL error 0x0502 in /Users/andres/Documents/Projects/MetroMon/frameworks/runtime-src/proj.android/…/…/cocos2d-x/cocos/./renderer/CCTextureAtlas.cpp drawNumberOfQuads 693
04-23 19:38:50.492: W/Adreno-ES20(31595): <gl_draw_error_checks:614>: GL_INVALID_OPERATION
04-23 19:38:50.492: D/cocos2d-x debug info(31595): OpenGL error 0x0502 in /Users/andres/Documents/Projects/MetroMon/frameworks/runtime-src/proj.android/…/…/cocos2d-x/cocos/./renderer/CCTextureAtlas.cpp drawNumberOfQuads 693

Does anyone have any idea how to fix this?
I’m using cocos v3.3

I just updated to cocos v3.5. Same problem…

I googled for the problem, and it seems that it’s normal in Android to destroy and create activities when going to the background, but it doesn’t seem that cocos2d supports it. Doesn’t anyone else experience this problem? I’m trying everything I can think of to workaround this problem buy can’t find a solution.

I’m trying now to modify onCreate so that cocos_android_app_init does not get called again, and the assert is not triggered. The rendering errors persist, and I think the problem might be that a new GLSurfaceView is created, and the existing instances (CCDirector, CCRenderer) might not like it. Maybe I have to initialise it with CCRenderer again somehow?

Any help would be appreciated, thanks!

I have the same problem in v2.2.6, but I didn’t fix it. I just keep my screen unlocked when it launches in debug mode.

some references, hope them help:
http://kyokomi.hatenablog.com/entry/2013/08/30/230124
http://discuss.cocos2d-x.org/t/sm-psharedapplication-is-false-on-specific-android-smartphone/16503/3

We are seeing a problem with this as well. There are no OpenGL errors in the log, but there is a big problem with flickering images etc after onCreate is called the second time.

Does anyone know how to solve this problem?

It appears this solves the problem:

diff --git a/cocos2d/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxActivity.java b/cocos2d/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxActivity.java
index 3c749ae…ec9b880 100644
— a/cocos2d/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxActivity.java
+++ b/cocos2d/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxActivity.java
@@ -259,6 +259,12 @@ public abstract class Cocos2dxActivity extends Activity implements Cocos2dxHelpe
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
+

  •           if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
    
  •                   finish();
    
  •                   return;
    
  •           }
    
  •    CocosPlayClient.init(this, false);