Adds Input Focus handling on Android to improve pausing/resuming behavior
authorGabriel Jacobo <gabomdq@gmail.com>
Tue, 09 Jul 2013 10:25:16 -0300
changeset 7384 07c5fcdd3cad
parent 7383 97d080ba383f
child 7385 6e90315f859a
Adds Input Focus handling on Android to improve pausing/resuming behavior Ref: http://android-developers.blogspot.com/2011/11/making-android-games-that-play-nice.html
android-project/src/org/libsdl/app/SDLActivity.java
--- a/android-project/src/org/libsdl/app/SDLActivity.java	Mon Jul 08 23:37:00 2013 -0400
+++ b/android-project/src/org/libsdl/app/SDLActivity.java	Tue Jul 09 10:25:16 2013 -0300
@@ -28,7 +28,7 @@
     private static final String TAG = "SDL";
 
     // Keep track of the paused state
-    public static boolean mIsPaused = false, mIsSurfaceReady = false;
+    public static boolean mIsPaused = false, mIsSurfaceReady = false, mHasFocus = true;
 
     // Main components
     protected static SDLActivity mSingleton;
@@ -94,6 +94,18 @@
         SDLActivity.handleResume();
     }
 
+
+    @Override
+    public void onWindowFocusChanged(boolean hasFocus) {
+        super.onWindowFocusChanged(hasFocus);
+        Log.v("SDL", "onWindowFocusChanged(): " + hasFocus);
+
+        SDLActivity.mHasFocus = hasFocus;
+        if (hasFocus) {
+            SDLActivity.handleResume();
+        }
+    }
+
     @Override
     public void onLowMemory() {
         Log.v("SDL", "onLowMemory()");
@@ -139,7 +151,7 @@
      * every time we get one of those events, only if it comes after surfaceDestroyed
      */
     public static void handleResume() {
-        if (SDLActivity.mIsPaused && SDLActivity.mIsSurfaceReady) {
+        if (SDLActivity.mIsPaused && SDLActivity.mIsSurfaceReady && SDLActivity.mHasFocus) {
             SDLActivity.mIsPaused = false;
             SDLActivity.nativeResume();
             mSurface.enableSensor(Sensor.TYPE_ACCELEROMETER, true);