Close app on Android Back Button

How to close the app, when back button is pressed (in Android):

Tried the solution given in following link:

But not working for me… m using Cocos2d-JS v3.0 Final

Any hints would be highly appreciated :slight_smile:

This solution is worked for me in version Cocos2d-JS v3.0 Final.

Create back button listener: (for example in layer ctor)

if(cc.sys.isNative && cc.sys.os == cc.sys.OS_ANDROID)
    this.createBackButtonListener();

Back button listener:

createBackButtonListener: function(){
    var self = this;

    cc.eventManager.addListener({
        event: cc.EventListener.KEYBOARD,

        onKeyReleased:function(key, event) {
            if(key == cc.KEY.back){
                cc.director.end(); //this will close app
            }
        }
    }, this);
}
4 Likes

thanks… working awesome :smile:

Hi ,can you tel how to call js method from java/android in cocos2d-js?
Pls help me …

http://cocos2d-x.org/docs/manual/framework/html5/v3/reflection/en

The last paragraph What’s more

@Kirils_Buls : thanks.I tried calling java/android method from js code,it worked perfectly. But when tried to call js method from android/java code ,it was not working, force close occured. So pls help me how to call js method from android/java code …

Show your code where you call js from Java.

sure ,the below code i wrote in AppActivity

public class AppActivity extends Cocos2dxActivity
{
private static AppActivity app = null;

 static String hostIPAdress="0.0.0.0";
 protected void onCreate(Bundle savedInstanceState) 
 {
       super.onCreate(savedInstanceState);
        app = this;
   .........................
   ............................
   }
public static void showAlertDialog() 
{
app.runOnUiThread(new Runnable() 
{
	@Override
	public void run()
	{
		AlertDialog alertDialog = new AlertDialog.Builder(app).create();
		alertDialog.setTitle("Alert Box");
		alertDialog.setMessage("This is your message");
		alertDialog.setIcon(R.drawable.icon);
		alertDialog.setButton("OK", new DialogInterface.OnClickListener()
		{
			public void onClick(DialogInterface dialog, int which) 
			{


				app.runOnGLThread(new Runnable() 
				{
					@Override
					public void run() 
					{
						   Cocos2dxJavascriptJavaBridge.evalString("HelloWorldLayer.prototype.javaCallBack();");// calling the js method ......
					}
				});
			}
		});
		alertDialog.show();
	}
});

}



}

i aslo added "Cocos2dxJavascriptJavaBridge.java " class file in src folder of this project
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
the below is js code
var HelloWorldLayer = cc.Layer.extend({
size:null,
Count:null,
ctor:function ()
{
this._super();

    size = cc.winSize;
    Count=0;
    
  
   var closeItem = new cc.MenuItemImage(
        res.CloseNormal_png,
        res.CloseSelected_png,
        function ()
		{
        	if(cc.sys.isNative && cc.sys.os == cc.sys.OS_ANDROID)
        	{
        		jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "showAlertDialog", "()V");// calling java code 
        	}
         }, this);
    closeItem.attr({
        x: size.width - 20,
        y: size.height/2,
        anchorX: 0.5,
        anchorY: 0.5
    });
   
    var menu = new cc.Menu(closeItem);
    menu.x = 0;
    menu.y = 0;
    this.addChild(menu, 1);

     helloLabel = new cc.LabelTTF("Hello", "Arial", 38);
    helloLabel.x = size.width / 2;
    helloLabel.y = size.height - 40;
    this.addChild(helloLabel, 5);
    return true;
},
javaCallBack:function(){

	Count+=10;
	cc.log(Count);
	helloLabel.setString(Count);
	cc.log("javaCallBack is running in app.js");// output javaCallBack is running
	
}

});

var HelloWorldScene = cc.Scene.extend({
onEnter:function () {
this._super();
var layer = new HelloWorldLayer();
this.addChild(layer);
}
});