cocos2d-x resume from background issue

I love cocos2d-x, it helps me to write games quickly on iOS and android with just one code base.

But I found a small problem that if my cocos2d-x games run into background, and when it get back to foreground, it will restart !

That means if you play my game for a while and a phone call just received, after you pick up the call, you will need to re-play the game from the very first stage.

That’s bad, and I don’t like it !

After a while googling, I found the solution, and I post here to make sure if someone have the same issue with me, and you can try this solution, it works for me, and maybe you too !

Step1 : open the AppDelegate.cpp

Step2 : make your code like below

// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground()
{
    CCDirector::sharedDirector()->stopAnimation();
    CCDirector::sharedDirector()->pause();

 // if you use SimpleAudioEngine, it must be pause
 CocosDenshion::SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
}

// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground()
{
    CCDirector::sharedDirector()->stopAnimation();
    CCDirector::sharedDirector()->resume();
    CCDirector::sharedDirector()->startAnimation();

 // if you use SimpleAudioEngine, it must resume here
 CocosDenshion::SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
}

Step3 : re-compile and run it

And that’s all !

Hope you will enjoy your coding time !

It also posts on my website as usual:

Thank you for sharing.

sorry…I don’t know how to well-form the codes on forum.

If you can tell me, I would like to modify that to improve the readability. _

Of course, include your codes in  

thanks, updated ! _

My game does not restart at all. And i use the default code. It only restarts, when something breaks when coming back, like an memory error or something i did wrong. Maybe you are crashing when returning ?

This might have fixed your problem, but so far it doesnt mean its a solution for a common scenario. My game does only calls for pause and resume and it works as expected. How have you figured it out ? Maybe you had issues when pausing your schedulers or animations…

Just curious on how it helped you! Thanks for sharing! :slight_smile:

Hi,

May I ask what version that you use ?

I do have this problem, and I don’t think that only cocos2d-x have this situation, at the cocos2d-iphone forum, there are some comments about this.

Thanks It’s works for me

You’re welcome !

Glad to heard that help someone indeed ! :slight_smile:

Thanks.
It worked for me too.
I had this problem with cocosDeshion.

In my app (in windows phone 8), when app goes into background, and then it get back to foreground some images and drawing and action not show or show unaccepted behave.
what can i do for it ?

@sharma_arpit
What’s the engine version?

cocos2d-x 3.0rc1

When you run Cpp-test and start any action then press screen lock or long back press in windows phone app become in background and then unlock screen. Image of next arrow won’t display.

Thanks for reporting.
We will checkout it.

Thank you.

Hey guys, I’m experiencing similar issues with Cocos2d-X 3.2. When the app goes into the background, and is then restored at some later time, two issues occur:

  1. The text (TTF labels) turn into solid squares and is no longer readable (Android).
  2. The app restarts. On Android it tells me that the app has crashed, and then it turns off, on iOS, the app simply restarts.

Please help me figure this out as it is driving me nuts!

Yes ,I also have the same issue, when app comes into foreground, Label TTF turns into blackboxes(sometimes),
Did you find any solution? Please reply