Problem with back button android

Hi,
i have implemented the back button android using coco2d-x V3.0, like this:

HelloWorld.h
 
void onKeyReleased( cocos2d::EventKeyboard::KeyCode keycode, cocos2d::Event *event );

HelloWorld.cpp

 this->setKeypadEnabled( true );

void HelloWorld::onKeyReleased( cocos2d::EventKeyboard::KeyCode keycode, cocos2d::Event *event )
{
	Director::getInstance()->end();
}

but now if i touch the back button and i lock the screen, the application didn’t exit complitly, and will crash when i unlock the screen !
Could you please help me to deal with this problem ?

void HelloWorld::onKeyReleased( cocos2d::EventKeyboard::KeyCode keycode, cocos2d::Event *event )
{
    if (keyCode == EventKeyboard::KeyCode::KEY_ESCAPE){
	Director::getInstance()->end();
    }
}

You don’t need keypad for it.

I’m sorry I can say nothing for crash thing.

Any help please ?

There are many possibilities for crash.
Look carefully when app crashes from adb logcat.

When i made this condition

if (keyCode == EventKeyboard::KeyCode::KEY_ESCAPE){
Director::getInstance()->end();
}
i have no reaction !

Then you can create EventListner for keyboard at onEnter or init().

_listenerKeyboard = EventListenerKeyboard::create();
_listenerKeyboard->onKeyReleased = CC_CALLBACK_2(HellowWorld::onKeyReleased, this);
getEventDispatcher()->addEventListenerWithSceneGraphPriority(_listenerKeyboard, this);

You should see what’s the difference between Node and Layer.

Thanks, but does it work only for the physic button or also the back touch on the screen