Pause scene after entering in foreground

Hi,
I am beginner of cocos2d-x. I am developing in C++. My game is 90% ready but I am stucked at one place.
My game code is in HelloWorld class, where player is hitting the enemies.
My Problem is:
condition 1:
I am displaying resume Layer when user press pause button. If from here, user is going in background and coming back, user can see the resume Layer.
condition 2:
If suppose user is playing the game. If from here, user is going in background and coming back, resume layer is displaying but layer is not paused.

I am also using notification to display resume Layer.

My AppDelegate.cpp code is


void AppDelegate::applicationDidEnterBackground()
{
    CCLog("applicationDidEnterBackground");

    CCDirector::sharedDirector()->stopAnimation();
    CCDirector::sharedDirector()->pause();
    
    SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
}

void AppDelegate::applicationWillEnterForeground()
{
    
    CCLog("applicationWillEnterForeground");

    CCDirector::sharedDirector()->stopAnimation();

    
    if (CCUserDefault::sharedUserDefault()->getBoolForKey("App_Paused")) {
        CCDirector::sharedDirector()->pause();
    }
 else{
       
        CCDirector::sharedDirector()->startAnimation();
        CCNotificationCenter::sharedNotificationCenter()->postNotification("APP_IN_BACKGROUND");
        CCDirector::sharedDirector()->pause();

    }
    
    SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
    
}

And in My HelloWorld class I am doing this things:


//// adding observer
void HelloWorld::onEnter()
{
   CCNotificationCenter::sharedNotificationCenter()->addObserver(this, callfuncO_selector(HelloWorld::pauseSceneBeforeBackground), "APP_IN_BACKGROUND", NULL);
    CCLayer::onEnter();
}

/// removing observer
void HelloWorld::onExit()
{
    CCNotificationCenter::sharedNotificationCenter()->removeObserver(this, "APP_IN_BACKGROUND");
    CCLayer::onExit();
}

///// Method called on Notification
void HelloWorld::pauseSceneBeforeBackground(CCObject* obj)
{
    this->pauseScene();
}


///// on resume button touch
void HelloWorld::resumePlaying(CCObject *sender){

    CCDirector::sharedDirector()->startAnimation();
    CCDirector::sharedDirector()->resume();
    CCUserDefault::sharedUserDefault()->setBoolForKey("App_Paused", false);
    CCUserDefault::sharedUserDefault()->flush();
}

void HelloWorld::pauseScene(){
    
    CCDirector::sharedDirector()->pause();
    CCUserDefault::sharedUserDefault()->setBoolForKey("App_Paused", true);
    CCUserDefault::sharedUserDefault()->flush();
   
    ResumeLayer *resumeLayer = ResumeLayer::create();
    resumeLayer->setContentSize(winSize);
    this->addChild(resumeLayer);
    
}

I am loading resume layer in applicationWillEnterForeground method. If I am not starting animation in applicationWillEnterForeground ie @CCDirector::sharedDirector()->startAnimation();@ then resume Layer is not displayed.

Please suggest how I should call these functions to run it properly.
Thank you in advance and great job :slight_smile: