Android back button does not work

后端 未结 5 1132
星月不相逢
星月不相逢 2021-02-06 14:56

I am using cocos2dx to make a small game and in the activity of my game i give the following functions to handle back button.

@Override
 public boolean onKeyDown         


        
相关标签:
5条回答
  • 2021-02-06 15:24

    Have you enabled the touch? If not then please enable it and Hope, it'll sort-out your problem.

    I assume that's button in your game screen.

    0 讨论(0)
  • 2021-02-06 15:32

    To handle back button pressing you need to redefine onBackPressed() method of your activity, not this two methods.

    0 讨论(0)
  • 2021-02-06 15:39

    Just your apps implements for override method for onKeyDown,

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            // Here to implements for your code.
            Log.d(TAG, "KEYCODE_BACK");
        }
        return super.onKeyDown(keyCode, event);
    }
    
    0 讨论(0)
  • 2021-02-06 15:41

    Here's an update for Cocos2d-x version 3+

    This has been answered simply (and works) here

    as well as a slightly less complete youtube here

    0 讨论(0)
  • 2021-02-06 15:44

    It is been handled here in the file Cocos2dxGLSurfaceView.java

    change it to below, where myActivity is the cocos2dActicity

            case KeyEvent.KEYCODE_BACK:
                        AlertDialog ad = new AlertDialog.Builder(myActivity)
                        .setTitle("EXIT?")
                        .setMessage("Do you really want to exit?")
                        .setPositiveButton("YES", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                ((Cocos2dxActivity)myActivity).finish();
                            }
                        })
                        .setNegativeButton("NO", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
    
                            }
                        }).create();
                        ad.show();
                return true;
            case KeyEvent.KEYCODE_MENU:
    
    0 讨论(0)
提交回复
热议问题