[Android] Handle native thread finishing when not commanded from the Java side

This commit is contained in:
Gabriel Jacobo 2013-12-05 10:51:38 -03:00
parent 6c495a806a
commit 77d2d55e1e
3 changed files with 27 additions and 9 deletions

View file

@ -26,6 +26,7 @@ public class SDLActivity extends Activity {
// Keep track of the paused state // Keep track of the paused state
public static boolean mIsPaused = false, mIsSurfaceReady = false, mHasFocus = true; public static boolean mIsPaused = false, mIsSurfaceReady = false, mHasFocus = true;
public static boolean mExitCalledFromJava;
// Main components // Main components
protected static SDLActivity mSingleton; protected static SDLActivity mSingleton;
@ -63,6 +64,9 @@ public class SDLActivity extends Activity {
// Set up the surface // Set up the surface
mSurface = new SDLSurface(getApplication()); mSurface = new SDLSurface(getApplication());
// Make sure this variable is initialized here!
mExitCalledFromJava = false;
if(Build.VERSION.SDK_INT >= 12) { if(Build.VERSION.SDK_INT >= 12) {
mJoystickHandler = new SDLJoystickHandler_API12(); mJoystickHandler = new SDLJoystickHandler_API12();
} }
@ -115,6 +119,7 @@ public class SDLActivity extends Activity {
super.onDestroy(); super.onDestroy();
Log.v("SDL", "onDestroy()"); Log.v("SDL", "onDestroy()");
// Send a quit message to the application // Send a quit message to the application
SDLActivity.mExitCalledFromJava = true;
SDLActivity.nativeQuit(); SDLActivity.nativeQuit();
// Now wait for the SDL thread to quit // Now wait for the SDL thread to quit
@ -169,6 +174,12 @@ public class SDLActivity extends Activity {
} }
} }
/* The native thread has finished */
public static void handleNativeExit() {
SDLActivity.mSDLThread = null;
mSingleton.finish();
}
// Messages from the SDLMain thread // Messages from the SDLMain thread
static final int COMMAND_CHANGE_TITLE = 1; static final int COMMAND_CHANGE_TITLE = 1;
@ -616,6 +627,22 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
SDLActivity.mSDLThread = new Thread(new SDLMain(), "SDLThread"); SDLActivity.mSDLThread = new Thread(new SDLMain(), "SDLThread");
enableSensor(Sensor.TYPE_ACCELEROMETER, true); enableSensor(Sensor.TYPE_ACCELEROMETER, true);
SDLActivity.mSDLThread.start(); SDLActivity.mSDLThread.start();
// Set up a listener thread to catch when the native thread ends
new Thread(new Runnable(){
public void run(){
try {
SDLActivity.mSDLThread.join();
}
catch(Exception e){}
finally{
// Native thread has finished
if (! SDLActivity.mExitCalledFromJava) {
SDLActivity.handleNativeExit();
}
}
}
}).start();
} }
} }

View file

@ -425,11 +425,7 @@ main(int argc, char *argv[])
} }
SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK); SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
#ifdef ANDROID
exit(0);
#else
return 0; return 0;
#endif
} }
#else #else

View file

@ -287,11 +287,6 @@ main(int argc, char *argv[])
} }
SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK); SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
#ifdef ANDROID
exit(0);
#else
return 0;
#endif
} }
#else #else