Skip to content

Commit 1473991

Browse files
author
James Chen
committed
Merge pull request #3903 from Dhilan007/di2899
issues #2899:Fix application will crash when back to background on Andro...
2 parents d87c2fe + c854feb commit 1473991

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
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 s_threadKey;
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 (&s_threadKey, 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(s_threadKey) == NULL)
110+
pthread_setspecific(s_threadKey, _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: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -215,19 +215,19 @@ 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 s_methodInitialized = false;
218219
static void dispatch_pending_runnables() {
219220
static cocos2d::JniMethodInfo info;
220-
static bool initialized = false;
221-
222-
if (!initialized) {
223-
initialized = cocos2d::JniHelper::getStaticMethodInfo(
221+
222+
if (!s_methodInitialized) {
223+
s_methodInitialized = cocos2d::JniHelper::getStaticMethodInfo(
224224
info,
225225
"org/cocos2dx/lib/Cocos2dxHelper",
226226
"dispatchPendingRunnables",
227227
"()V"
228228
);
229229

230-
if (!initialized) {
230+
if (!s_methodInitialized) {
231231
LOGW("Unable to dispatch pending Runnables!");
232232
return;
233233
}
@@ -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+
s_methodInitialized = false;
656656
return;
657657
}
658658
}

0 commit comments

Comments
 (0)