diff -r 36c04a8c6545 android-project/src/org/libsdl/app/SDLActivity.java --- a/android-project/src/org/libsdl/app/SDLActivity.java Wed Jun 05 23:11:20 2013 -0700 +++ b/android-project/src/org/libsdl/app/SDLActivity.java Thu Jun 06 20:31:34 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, mIsResumingFromPause = false; // Main components protected static SDLActivity mSingleton; @@ -83,14 +83,42 @@ protected void onPause() { Log.v("SDL", "onPause()"); super.onPause(); - // Don't call SDLActivity.nativePause(); here, it will be called by SDLSurface::surfaceDestroyed + if (!SDLActivity.mIsPaused) { + SDLActivity.mIsPaused = true; + SDLActivity.mIsResumingFromPause = false; + SDLActivity.nativePause(); + } + // Maybe the surface does not get destroyed (e.g. during a screen lock) + // so it's safer to disable the sensor right now + if (SDLActivity.mSurface.getHolder().getSurface().isValid()) { + mSurface.enableSensor(Sensor.TYPE_ACCELEROMETER, false); + } } @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 + if (SDLActivity.mSurface.getHolder().getSurface().isValid()) { + // Maybe it has been disabled from onPause only + // (not surfaceDestroyed) so enable here + mSurface.enableSensor(Sensor.TYPE_ACCELEROMETER, true); + } + if (SDLActivity.mIsPaused) { + SDLActivity.mIsPaused = false; + SDLActivity.mIsResumingFromPause = true; + } + SDLActivity.handleResume(); + } + + /** Called by onResume or surfaceChanged; An actual + * resume can be done only when the surface is ready. + */ + public static void handleResume() { + if (SDLActivity.mIsResumingFromPause && SDLActivity.mSurface.getHolder().getSurface().isValid()) { + SDLActivity.mIsResumingFromPause = false; + SDLActivity.nativeResume(); + } } @Override @@ -232,16 +260,8 @@ 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; - } + } else { + SDLActivity.handleResume(); } } @@ -540,10 +560,6 @@ @Override public void surfaceDestroyed(SurfaceHolder holder) { Log.v("SDL", "surfaceDestroyed()"); - if (!SDLActivity.mIsPaused) { - SDLActivity.mIsPaused = true; - SDLActivity.nativePause(); - } enableSensor(Sensor.TYPE_ACCELEROMETER, false); }