Making 'Back' button close the application in Android with Cocos2dx

With the default Cocos2dx template the back button on your phone won't close the application. Without this your app will fail the test procedure for some curated stores (eg Samsung). Here's how I did it.

Firstly, you need to decide how you want to display a 'Do you want to quit' type money. There are three options I guess, showing an extra menu on your 'HelloWorld' scene, calling a native java dialog, or showing a 'Quit' layer. I decided on the latter as it would be easy to reuse the code. So I made a very basic called call 'QuitLayer' as follows:

Here's the header:
#ifndef __QUIT_SCENE_H__
#define __QUIT_SCENE_H__

#include "cocos2d.h"

class QuitLayer : public cocos2d::CCLayer
virtual bool init();
    static cocos2d::CCScene* scene();
    void goQuit(CCObject* pSender);
    void goBack(CCObject* pSender);

    // implement the "static node()" method manually


Here's the class. There's nothing very clever here - it uses graphics called 'quitdialog.png' 'butyes.png' etc to construct the dialog box.

If you click 'Yes' (to quit) it calls:


Which ends the app.

#include "QuitLayer.h"
#include "SimpleAudioEngine.h"

#include "HelloWorldScene.h"
using namespace cocos2d;
using namespace CocosDenshion;

static float scale = 0.5;

static float screenWidth;
static float screenHeight;

CCScene* QuitLayer::scene()
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    // 'layer' is an autorelease object
    QuitLayer *layer = QuitLayer::create();
    // add layer as a child to scene
    // return the scene
    return scene;

// on "init" you need to initialize your instance
bool QuitLayer::init()
    // 1. super init first
    if ( !CCLayer::init() )
        return false;

    CCSize size = CCDirector::sharedDirector()->getWinSize();
    screenWidth = size.width;
    screenHeight = size.height;
   pSprite = CCSprite::create("introback.png");
    pSprite->setPosition( ccp(size.width/2, size.height/2) );
    this->addChild(pSprite, 0);

    CCSprite * quitBox = CCSprite::create("quitdialog.png");
    int fontsize = 64*scale;

    // Button menu
    CCMenuItem * butYes= CCMenuItemImage::create("butyes.png"
                                               menu_selector(QuitLayer::goQuit) );
    CCMenuItem * butNo= CCMenuItemImage::create("butno.png"
    CCMenu *Menu = CCMenu::create(butYes,butNo,NULL);
    Menu->setPosition(ccp(screenWidth/2, screenHeight/2));
 return true;

void QuitLayer::goQuit(CCObject* pSender)

void QuitLayer::goBack(CCObject* pSender)
    CCScene *pScene = HelloWorld::scene();

You'll now need to call this from 'HelloWorld' (or whatever you called your first scene) when the back button is pressed. That's actually very easy.

Add a method called 'keyBackClicked' to call your Quit scene when the button is pressed.

To HelloWorldScene.h add

   void keyBackClicked();

To HelloWorldScene add.

void HelloWorld::keyBackClicked()

    CCScene *pScene = QuitLayer::scene();

Finally, you need to enable the keypad. So put something like this in your init in HelloWorld:


1 comment: