applicationWillEnterForeground and applicationDidEnterBackground are not being called

Hello:

I am trying to develop a game for the plabook with the corresponding version of cocos2dx, but after make some sound test I noticed that when the application goes to the background the sound was not stopped since the method ‘applicationDidEnterBackground’ is not being called.
There is something extra that I should be doing to get this methods called or this functionality is not in course of implementation yet?

Thank you in advance and great job dudes :slight_smile:

Hello

I noticed the same issue on IOS.I am on version 1.0.1-x-0.9.2.
These two methods are never called.

Thanks!

Hello

I have already implemented the solution, what I did is(in case it can help someone else)

1.- Define the pre-processor flag ‘BPS_EVENTS’
2.- Open the file “CCEGLView_qnx.cpp”
3.- Search the location in which its getting the navigator window events with this function “bps_event_get_code(event)”
4.- You will notice that there are a switch evaluating the return value of this function, add to the case options the next code:

@
case NAVIGATOR_WINDOW_ACTIVE:
CCApplication::sharedApplication().applicationWillEnterForeground();
break;
case NAVIGATOR_WINDOW_INACTIVE:
CCApplication::sharedApplication().applicationDidEnterBackground();
break;
case NAVIGATOR_WINDOW_STATE:
{
switch(navigator_event_get_window_state(event))
{
case NAVIGATOR_WINDOW_FULLSCREEN:
CCApplication::sharedApplication().applicationWillEnterForeground();
break;
case NAVIGATOR_WINDOW_THUMBNAIL:
CCApplication::sharedApplication().applicationDidEnterBackground();
break;
}
break;
}
@
this will call the ‘applicationWillEnterForeground’ when the application returns from idle or from the background and will call ‘applicationDidEnterBackground’ when the device goes to idle mode or when it get re-sized to a the thumbnail mode by the OS before sending it to the background

5.- Also add #include “CCApplication.h” in the beginning of this class

Hope it can help some else, and if some one knows a better way to do it please let me know :slight_smile:

Is this method for IOS or for other platform ?
I don’t find CCEGLView_qnx.cpp file on my IOS cocos2d-x folder.

Thanks!
Adrian

Hi, German Martinez,
Could you please make a ‘pull request’ on github? Refer to https://github.com/cocos2d/cocos2d-x
You should fork cocos2d-x project first, and clone the project from your remote repository, then make your changes locally and push to your remote repo, click ‘Pull Request’ button to send us the code.
Thanks.

Hi:

Adrian: Sorry this implementation is only for the QNX version (Black Berry Playbook)James Chen: The pull request is done :slight_smile:

Have an excellent day.

OK, I will review your code and test it on my playbook, thanks!

German Martinez wrote:

Hi:
>
Adrian: Sorry this implementation is only for the QNX version (Black Berry Playbook)James Chen: The pull request is done :slight_smile:
>
Have an excellent day.

Can someone please try to make this work on IOS as well?
Thanks!

Hi, GermanMtz,
dducharme who works in rim company has done a ‘pull request’ to us, and we merge his code, I am sorry I can not merge your code automatically by github. So could you please pull the latest code and do a pull request again?
BTW, I notice the code you modify as follow:

                case NAVIGATOR_WINDOW_INACTIVE:
                    CCApplication::sharedApplication().applicationDidEnterBackground();
                    break;

                case NAVIGATOR_WINDOW_ACTIVE:
                    CCApplication::sharedApplication().applicationWillEnterForeground();
                    break;
                case NAVIGATOR_WINDOW_STATE:
                {
                    switch(navigator_event_get_window_state(event))
                    {
                    case NAVIGATOR_WINDOW_FULLSCREEN:
                        CCApplication::sharedApplication().applicationWillEnterForeground();
                        break;
                    case NAVIGATOR_WINDOW_THUMBNAIL:
                        CCApplication::sharedApplication().applicationDidEnterBackground();
                        break;
                    }
                    break;
                }

applicationWillEnterForeground will be invoked twice when application is launched, one is from NAVIGATOR_WINDOW_ACTIVE and another is from NAVIGATOR_WINDOW_FULLSCREEN? Any good way to avoid this?

Thanks very much!

Hi:

I have already fetched the new changes and updated the fix to invoke the functions ‘applicationWillEnterForeground’ and ‘applicationDidEnterBackground’ only once per time.

Any other comment please let me know
and have an excellent day

I don’t think using the isPaused method of CCDirector is a good way.
Because it depends on whether user invokes ‘CCDirector::sharedDirector()->pause();’ in the ‘void AppDelegate::applicationDidEnterBackground()’ . I think it’s a good way by just using a member variable of CCEGLView to control this logic. Do you think so ?

Hi all , I got the same problems with the AppDelegate::applicationWillEnterForeground() and AppDelegate::applicationDidEnterBackground() for the Music.
so is there a good way to solve it ?

thanks

For the Playbook version is already resolved in the last code version
The methods AppDelegate::applicationWillEnterForeground() and AppDelegate::applicationDidEnterBackground() get called whenever your application goes to the back and return to the front.

Inside that methods you have to insert the code to pause the sound engine
ie: SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();

Thanks German Martinez for the reply,

I’m already try the lastest code from Github which is cocos2d-cocos2d-x-9ffc9d9 version.
And I’ve already change my mp3 background music to ogg format, but I meet some problem when try to pause and resume the game.

this in my AppDelegate.cpp :

// 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()->pause();

// if you use SimpleAudioEngine, it must be pause
SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
SimpleAudioEngine::sharedEngine()->pauseAllEffects();
CCLog(“PAUSE”);
}

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

// if you use SimpleAudioEngine, it must resume here
SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
SimpleAudioEngine::sharedEngine()->resumeAllEffects();
CCLog(“RESUME”);
}

When I try to pause game the applicationDidEnterBackground() got called ,
but before I resume game the applicationWillEnterForeground() got called too
so I got this in my console when I try to pause the game :

cocos2d-x debug info [PAUSE]
cocos2d-x debug info [RESUME]

any advice ?

thanks

First of all, in which platform are you getting this issue?
I mention this beacause previously I thoght you where refering to the playbook, but as you mentioned that “When comes a phone call,it’s be invoked too” I suppose that you are developing to another platform.

In the playbook when debugging the backgrounds calls I receive them correctly applicationDidEnterBackground when goind to the background and applicationWillEnterForeground when returning (not before).

If you are receiving a strange behavior you can check the file cocos2dx/platform//CCEGLView_.cpp
From this file is from where the call to applicationDidEnterBackground and applicationWillEnterForeground are done

Thanks for the reply German Martinez

I’m using the QNX platform and test it on the simulator,

I try to put some log in the CCEGLView_qnx.cpp in :

case NAVIGATOR_WINDOW_INACTIVE:
if(m_isWindowActive)
{
CCApplication::sharedApplication().applicationDidEnterBackground();
m_isWindowActive = false;
CCLog(“INACTIVE”);
}
break;

case NAVIGATOR_WINDOW_ACTIVE:
if(!m_isWindowActive)
{
CCApplication::sharedApplication().applicationWillEnterForeground();
m_isWindowActive = true;
CCLog(“ACTIVE”);
}
break;

case NAVIGATOR_WINDOW_FULLSCREEN:
if(!m_isWindowActive)
{
CCApplication::sharedApplication().applicationWillEnterForeground();
m_isWindowActive = true;
CCLog(“ACTIVE 2”);
}
break;
case NAVIGATOR_WINDOW_THUMBNAIL:
if(m_isWindowActive)
{
CCApplication::sharedApplication().applicationDidEnterBackground();
m_isWindowActive = false;
CCLog(“INACTIVE 2”);
}
break;

and this the log I get in the console when I pause the game :

cocos2d-x debug info [INACTIVE 2]
cocos2d-x debug info [ACTIVE 2]

when I’m pause the game the switch case got NAVIGATOR_WINDOW_FULLSCREEN and the CCApplication::sharedApplication().applicationWillEnterForeground(); is called

anyone meet the same problem ?

thanks