diff -r 37b77749523b android-project/src/org/libsdl/app/SDLActivity.java --- a/android-project/src/org/libsdl/app/SDLActivity.java Mon Jun 24 22:06:50 2013 -0700 +++ b/android-project/src/org/libsdl/app/SDLActivity.java Mon Jul 01 17:24:54 2013 +0300 @@ -28,7 +28,7 @@ private static final String TAG = "SDL"; // Keep track of the paused state - public static boolean mIsPaused = false; + public static boolean mIsPaused = false, mIsSurfaceReady = false; // Main components protected static SDLActivity mSingleton; @@ -83,14 +83,14 @@ protected void onPause() { Log.v("SDL", "onPause()"); super.onPause(); - // Don't call SDLActivity.nativePause(); here, it will be called by SDLSurface::surfaceDestroyed + SDLActivity.handlePause(); } @Override protected void onResume() { Log.v("SDL", "onResume()"); super.onResume(); - // Don't call SDLActivity.nativeResume(); here, it will be called via SDLSurface::surfaceChanged->SDLActivity::startApp + SDLActivity.handleResume(); } @Override @@ -120,6 +120,31 @@ } } + + /** Called by onPause or surfaceDestroyed. Even if surfaceDestroyed + * is the first to be called, mIsSurfaceReady should still be set + * to 'true' during the call to onPause (in a usual scenario). + */ + public static void handlePause() { + if (!SDLActivity.mIsPaused && SDLActivity.mIsSurfaceReady) { + SDLActivity.mIsPaused = true; + SDLActivity.nativePause(); + mSurface.enableSensor(Sensor.TYPE_ACCELEROMETER, false); + } + } + + /** Called by onResume or surfaceCreated. An actual resume + * should be done only when the surface is ready. + */ + public static void handleResume() { + if (SDLActivity.mIsPaused && SDLActivity.mIsSurfaceReady) { + SDLActivity.mIsPaused = false; + SDLActivity.nativeResume(); + mSurface.enableSensor(Sensor.TYPE_ACCELEROMETER, true); + } + } + + // Messages from the SDLMain thread static final int COMMAND_CHANGE_TITLE = 1; static final int COMMAND_UNUSED = 2; @@ -227,24 +252,6 @@ return mSingleton; } - public static void startApp() { - // Start up the C app thread - if (mSDLThread == null) { - mSDLThread = new Thread(new SDLMain(), "SDLThread"); - mSDLThread.start(); - } - else { - /* - * Some Android variants may send multiple surfaceChanged events, so we don't need to resume every time - * every time we get one of those events, only if it comes after surfaceDestroyed - */ - if (mIsPaused) { - SDLActivity.nativeResume(); - SDLActivity.mIsPaused = false; - } - } - } - static class ShowTextInputTask implements Runnable { /* * This is used to regulate the pan&scan method to have some offset from @@ -533,18 +540,17 @@ public void surfaceCreated(SurfaceHolder holder) { Log.v("SDL", "surfaceCreated()"); holder.setType(SurfaceHolder.SURFACE_TYPE_GPU); - enableSensor(Sensor.TYPE_ACCELEROMETER, true); + // Set mIsSurfaceReady to 'true' *before* any call to handleResume + SDLActivity.mIsSurfaceReady = true; } // Called when we lose the surface @Override public void surfaceDestroyed(SurfaceHolder holder) { Log.v("SDL", "surfaceDestroyed()"); - if (!SDLActivity.mIsPaused) { - SDLActivity.mIsPaused = true; - SDLActivity.nativePause(); - } - enableSensor(Sensor.TYPE_ACCELEROMETER, false); + // Call this *before* setting mIsSurfaceReady to 'false' + SDLActivity.handlePause(); + SDLActivity.mIsSurfaceReady = false; } // Called when the surface is resized @@ -603,7 +609,16 @@ SDLActivity.onNativeResize(width, height, sdlFormat); Log.v("SDL", "Window size:" + width + "x"+height); - SDLActivity.startApp(); + // Set mIsSurfaceReady to 'true' *before* making a call to handleResume + SDLActivity.mIsSurfaceReady = true; + // Start up the C app thread and enable sensor input for the first time + if (SDLActivity.mSDLThread == null) { + SDLActivity.mSDLThread = new Thread(new SDLMain(), "SDLThread"); + enableSensor(Sensor.TYPE_ACCELEROMETER, true); + SDLActivity.mSDLThread.start(); + } else { // App window is restored or a similar event occurs + SDLActivity.handleResume(); + } } // unused