Android - Application does not quit / does not terminate

Hello,

First of all, a very grateful thanks for creating this wonderful implementation. Since we use Cocos2D for iOS, it was a very obvious choice to use Cocos2D-x for Android.

We worked with the code migration from Objective C to C++ and it was very smooth … except for one irritation. :frowning:

Problem: The problem that we face now is that on an Android device, the application will not quit (application does not end, it does not close but instead it keeps running) when the Home button is pressed. It happens on an Android tablet 2.2. There is only one button - the Home button. I have to reboot the tablet to end the application.

Even keeping an “x” image button (as given in HelloWorld program) inside the program screen (which calls sharedDirector::end() ) does not respond till touched 10 to 15 times, over 10 seconds. This can become an irritation for end users.

On an Android 2.3 phone, the app does not respond to a back button, Home button works by sending it to background. Back button works for quitting other apps but not Cocos2d-x app.

Cocos2d-x version is 1.0.1-x-0.9.1. We used templates etc supplied with that zip.

Any inputs would be helpful. Where should I look or make changes ?

Something like changelist 626 (http://www.cocos2d-x.org/issues/626) could be of use in this case, but that was for Marmalade. This problem we have is for Android.

Any help please ?

Can you tell the exact name of “an android table 2.2” & “an Android 2.3 phone”, e.g. moto xxxx or samsung ixxx
Does it happen on all your android devices?

Hi Walzer,

Thanks for the response.

The Android Tablet is a Barnes and Noble “Nook Color” with Android 2.2. Home button does not work in a Cocos2d-x app. It works for all other apps.

The Android Phone is a Samsung Galaxy Pop S5570 with Android 2.3. Home button works, but Back button does not work in a Cocos2d-x app. Home and Back buttons work well for all other apps.

These are the 2 Android devices that I have.

Thanks,
P Mat

Hi,
for back button you must write your own code.
Simple overide virtual method:

virtual void keyBackClicked();

I am experiencing the same think with the Nook tablet. No back button, obviously, but pressing the home button has no effect. If I press it twice it will break out of the app but the normal behavior for a Nook app is that a single push of the home button will overlay the device’s menu. My code is a very slightly modified version of HelloWorld.

Thanks for all your hard work, it has been a steep learning curve but I now have my code running in Windows, Linux, and the Nook. Going to try and get iOS working tomorrow but very encouraged by the results thus far.

My B&N app got rejected today because of this reason. The home button does not work. Does anyone solve this problem? Please share your experience.

Emmy

You can refer the codes of HelloLua(HelloLua/android/src/org/cocos2dx/hellolua/HelloLua.java).
It will terminate itself when back menu is pressed.

Hi Minggo:

Thanks for your reply. B&N complains that clicking on the home button does not exit the app. I tried, when I double click the home button, the app exit. But the single click on home button, the app does not exit.

Here is my code.

ackage com.abitalk.sentencesbuilder;
import org.cocos2dx.lib.Cocos2dxActivity;
import org.cocos2dx.lib.Cocos2dxGLSurfaceView;

import android.os.Bundle;
import android.widget.EditText;
import android.view.KeyEvent;
import android.content.Context;
import android.opengl.GLSurfaceView;

public class SentencesBuilder extends Cocos2dxActivity{
private Cocos2dxGLSurfaceView mGLView;

protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);

// get the packageName,it’s used to set the resource path
String packageName = getApplication().getPackageName();
super.setPackageName(packageName);

setContentView(R.layout.game_demo);
mGLView = (Cocos2dxGLSurfaceView) findViewById(R.id.game_gl_surfaceview);
mGLView.setTextField((EditText)findViewById(R.id.textField));
}

`Override
protected void onPause() {
super.onPause();
mGLView.onPause();
}

 `Override

protected void onResume() {
super.onResume();
mGLView.onResume();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// exit program when key back is entered
if (keyCode == KeyEvent.KEYCODE_BACK) {
android.os.Process.killProcess(android.os.Process.myPid());
}
return super.onKeyDown(keyCode, event);
}
static {
System.loadLibrary(“cocos2d”);
System.loadLibrary(“cocosdenshion”);
System.loadLibrary(“game_logic”);
System.loadLibrary(“game”);
}
}

As I know, Android doesn’t terminate a program by pressing Home Menu.
You should do more thing to listen Home Menu touching event, should declare something in AndoridManifest.xml and write corresponding call back function.
I haven’t done it.
You can search solution by google.

I’ve come across this same issue as well, and it will certainly fail the B&N app submission standards.

In Marmalade, the framework actually captures the Nook home key event as an abstract back softkey, as s3eKeyAbsBSK. Normally you wouldn’t want to capture a home key event but the Nook is a special case, as the app will not take any action at all if the home key is pressed only once and we need it to suspend the app. I’ve tried overriding OnBackPressed and OnUserLeaveHint on the Java side, but that did not work.

Is there anything similar in Cocos2dx for Android? I don’t see any other way for an application to listen to the home key event without the framework supporting it.

Edit: Messing around a bit on the C++ side, Milda Genius’ solution worked!

My MainMenu inherits from cocos2d::CCLayer
I added a virtual void keyBackClicked(); in MainMenu.h
Then in MainMenu.cpp I added this->setIsKeypadEnabled(true); in MainMenu::init() and added a method:
void MainMenu::keyBackClicked() {
CCDirector::sharedDirector()->end();
}

And it worked. Wish there was an easier way but I’ll just have to add this to every layer.

Wow! Nice, I got try it out. I have two apps are pending from B&N because of this issue. I will let you know if it works or not on my NOOK

Hi B L:

Thank you so much. It works

Thank you!

Emmy

Hello guys , i put this code in my project , its ok now!

Another question is about background app crash after some time , every time i press home button and my app goes to background after 30 minutes more or less , its appear " Activity XXX , dos not respond"

My configuration of AppDelegate.cpp is

void AppDelegate::applicationDidEnterBackground()
{
CCDirector::sharedDirector()->pause();
}

// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground()
{
CCDirector::sharedDirector()->resume();
SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
GameManager::sharedGameManager()->getLevelAtual()->applicationWillEnterForeground = true;
}

Some tips?

thanks

This does not work in my appp. The function callback is not triggered. I put a CCLog call to check and it does not get hit. Any reason why this is happening?

in mine it does this error:

06-06 06:24:27.710: W/KeyCharacterMap(14179): Can’t open keycharmap file
06-06 06:24:27.710: W/KeyCharacterMap(14179): Error loading keycharmap file ‘/system/usr/keychars/sec_touchscreen_.kcm.bin’. hw.keyboards.65538.devname=‘sec_touchscreen’
06-06 06:24:27.710: W/KeyCharacterMap(14179): Using default keymap: /system/usr/keychars/qwerty.kcm.bin

when I press the back button at the first time, it shows the error:

06-06 19:18:44.740: W/KeyCharacterMap(1786): Can’t open keycharmap file
06-06 19:18:44.740: W/KeyCharacterMap(1786): Error loading keycharmap file ‘/system/usr/keychars/synaptics.kcm.bin’. hw.keyboards.131076.devname=‘synaptics’
06-06 19:18:44.740: W/KeyCharacterMap(1786): err is 0 dev is .
06-06 19:18:44.740: W/KeyCharacterMap(1786): Use qwerty.kcm.bin
06-06 19:18:44.740: W/KeyCharacterMap(1786): Using default keymap: /system/usr/keychars/qwerty.kcm.bin

and the function keyBackClicked wont execute;

when I press the back button at second time, the function keyBackClicked executes, and everything is fine

is there any thing I can do to solve this bug?

Got it OK using this solution: http://stackoverflow.com/questions/13697677/android-back-button-does-not-work

Got it OK using this solution: http://stackoverflow.com/questions/13697677/android-back-button-does-not-work

@ public boolean onKeyDown(final int pKeyCode, final KeyEvent pKeyEvent) {
Log.e(“onKeyDown”,“onKeyDownclicked”);
switch (pKeyCode) {
case KeyEvent.KEYCODE_BACK:
case KeyEvent.KEYCODE_MENU:
this.queueEvent(new Runnable() {
Override public void run() { Cocos2dxGLSurfaceView.this.mCocos2dxRenderer.handleKeyDown(pKeyCode); } }); return true; default: return super.onKeyDown(pKeyCode, pKeyEvent); } }

I debugged in Cocos2dxGLSurfaceView.java and find out the function onKeyDown does not even called at first time I press the back button. so wired

Hello,

I am creating an application using cocos2d-x 2.0.4 in xcode5. It is a timer application for particular time it is creating sounds. It is working fine. But when i lock my device the application goes to pause, and while unlock it resumes and start running from where it pause. So i want to know whether is it possible to make my application run on background. I convert this and using it for android. So please help me solve this. thanks.