Skip to content

Commit 5965f86

Browse files
committed
issues cocos2d#2899:Fix application will crash when back to background on Android.
1 parent bba5b27 commit 5965f86

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

cocos2dx/platform/android/jni/JniHelper.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ namespace cocos2d {
5858
jobject JniHelper::classloader = NULL;
5959
JNIEnv* JniHelper::env = NULL;
6060

61+
static pthread_key_t thread_key;
62+
6163
JavaVM* JniHelper::getJavaVM() {
6264
pthread_t thisthread = pthread_self();
6365
LOGD("JniHelper::getJavaVM(), pthread_self() = %X", thisthread);
@@ -72,6 +74,10 @@ namespace cocos2d {
7274
JniHelper::cacheEnv(javaVM);
7375
}
7476

77+
void JniHelper::detach_current_thread (void *env) {
78+
_psJavaVM->DetachCurrentThread();
79+
}
80+
7581
bool JniHelper::cacheEnv(JavaVM* jvm) {
7682
JNIEnv* _env = NULL;
7783
// get jni environment
@@ -89,7 +95,8 @@ namespace cocos2d {
8995
// TODO : If calling AttachCurrentThread() on a native thread
9096
// must call DetachCurrentThread() in future.
9197
// see: http://developer.android.com/guide/practices/design/jni.html
92-
98+
99+
pthread_key_create (&thread_key, JniHelper::detach_current_thread);
93100
if (jvm->AttachCurrentThread(&_env, NULL) < 0)
94101
{
95102
LOGD("Failed to get the environment using AttachCurrentThread()");
@@ -99,6 +106,8 @@ namespace cocos2d {
99106
} else {
100107
// Success : Attached and obtained JNIEnv!
101108
JniHelper::env = _env;
109+
if (pthread_getspecific(thread_key) == NULL)
110+
pthread_setspecific(thread_key, _env);
102111
return true;
103112
}
104113

cocos2dx/platform/android/jni/JniHelper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class CC_DLL JniHelper
6060
static jobject classloader;
6161

6262
private:
63+
static void detach_current_thread (void *env);
6364
static bool cacheEnv(JavaVM* jvm);
6465

6566
static bool getMethodInfo_DefaultClassLoader(JniMethodInfo &methodinfo,

cocos2dx/platform/android/nativeactivity.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,10 @@ static cocos_dimensions engine_init_display(struct engine* engine) {
215215
/**
216216
* Invoke the dispatching of the next bunch of Runnables in the Java-Land
217217
*/
218+
static bool initialized = false;
218219
static void dispatch_pending_runnables() {
219220
static cocos2d::JniMethodInfo info;
220-
static bool initialized = false;
221-
221+
222222
if (!initialized) {
223223
initialized = cocos2d::JniHelper::getStaticMethodInfo(
224224
info,
@@ -652,7 +652,7 @@ void android_main(struct android_app* state) {
652652
engine_term_display(&engine);
653653

654654
memset(&engine, 0, sizeof(engine));
655-
655+
initialized = false;
656656
return;
657657
}
658658
}

0 commit comments

Comments
 (0)