Android back button does not work
Asked Answered
A

5

5

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(int keyCode, KeyEvent event)
 {
     return super.onKeyDown(keyCode, event);
 }

 @Override
 public void onDestroy()
 {
     android.os.Process.killProcess(android.os.Process.myPid());
     super.onDestroy();
 }

On pressing back button i get the following warning in my logcat

Can't open keycharmap file

Error loading keycharmap file '/system/usr/keychars/qtouch-touchscreen.kcm.bin'. hw.keyboards.65538.devname='qtouch-touchscreen'

The call doesn't reach onKeyDown or onDestroy functions.

Please tell me why this warning is caused and why i cannot handle the android back button.

The functions work fine on my java android project but not in my cocos2d-x project

Antisana answered 4/12, 2012 at 7:1 Comment(0)
M
9

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:
Mullens answered 4/12, 2012 at 7:47 Comment(3)
It doesn't exit, when I run it again the continues from where it was.Wickham
How exactly do you get access to the activity here?Pill
@BrandonRomano please pass the main acticyty as an arguments when the Cocos2dxGLSurfaceView instance is been constructed in the main activity. you probably need a variable inside Cocos2dxGLSurfaceView.java to store it, in my case is myActivity.Mullens
H
2

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

Heterogamy answered 4/12, 2012 at 7:7 Comment(3)
I tried this too but call is not reaching 'onBackPressed()' method either.Antisana
It seems cocos caprute back pressing. Check this message, maybe this is what you are looking for cocos2d-x.org/boards/6/topics/5041?r=5059#message-5059Heterogamy
But this will help only if i am inheriting CCLayer which i am not. Is there any other way to solve this crisis.Antisana
C
1

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.

Coattail answered 4/12, 2012 at 7:4 Comment(1)
No i meant the back button on the android phone. The home button on phone works perfectly but the back button is not working.Antisana
W
1

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);
}
Wilkey answered 4/12, 2012 at 7:21 Comment(1)
Yes i am just overriding onKeyDown function but it works fine on my java android project. It just does not work in my cocos2d-x project.Antisana
M
1

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

Meli answered 9/10, 2014 at 17:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.