[JNI] java calling back into a CCLayer class problem

[JNI] java calling back into a CCLayer class problem
0.0 0


Hi all,
I have an issue where in my code it has to call an external app via the Intent/Activity protocol from the Java side of cocos2d-x/android. This then puts the external app on top of my game, which is all fine. I known when in my Java code when this app closes and the focus is back on my game. The problem is that even though the Java side knows its back in focus, how can I check in the cpp/CCLayer class that its back? onEnter() does not work as the game still thinks CCLayer is on the “stage” even with the external app in front.

I tried defining this within my calling CCLayer class:

#ifdef __cplusplus
extern "C" {
void MyClass::Java_com_game_class_MyMethod(JNIEnv *env, jobject thiz)
#ifdef __cplusplus

And this within my main Java class

    public native void MyMethod();

and I call MyMethod() when the extern app is closed, but I get this error/crash when its called.

04-17 15:12:12.851: E/AndroidRuntime(28048): java.lang.UnsatisfiedLinkError: MyMethod
04-17 15:12:12.851: E/AndroidRuntime(28048):    at com.game.class.MyMethod(Native Method)

So it looks that the Java can’t find MyMethod at all. How can I get this method exposed enough to the Java to execute it? Or is there a way to pass the CCLayer object into Java and Java to call a CCLayer method with that instance of the class?


For your first question, I believe the relevant method to implement is CCApplication::applicationWillEnterForeground(). It is usually inside the AppDelegate class if you used the wizard to set it up. If you use that, you should be able to find a way to propagate that to the currently displayed scene/layer.

JNI is a different problem; I can help you set that up too if the above solution doesn’t fit your issue.


Hi Irwin,

CCApplication::applicationWillEnterForeground() does sound like it would do the trick, but I think that brings in things where’d I have to check how the app came into the Foreground, etc. I would prefer the JNI way instead, as its more neater and controlled, but I could fall back to applicationWillEnterForeground() if JNI becomes a mess.