Improve Android pause/resume behavior.
authorGabriel Jacobo <gabomdq@gmail.com>
Fri, 29 Nov 2013 10:06:08 -0300
changeset 8039 3b0346b37e0f
parent 8038 e1f2a85d9dda
child 8041 afd62b3fda31
Improve Android pause/resume behavior.
src/core/android/SDL_android.c
src/video/android/SDL_androidevents.c
--- a/src/core/android/SDL_android.c	Thu Nov 28 02:31:32 2013 -0800
+++ b/src/core/android/SDL_android.c	Fri Nov 29 10:06:08 2013 -0300
@@ -300,16 +300,17 @@
 void Java_org_libsdl_app_SDLActivity_nativePause(
                                     JNIEnv* env, jclass cls)
 {
+    __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "nativePause()");
     if (Android_Window) {
-        /* Signal the pause semaphore so the event loop knows to pause and (optionally) block itself */
-        if (!SDL_SemValue(Android_PauseSem)) SDL_SemPost(Android_PauseSem);
         SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_FOCUS_LOST, 0, 0);
         SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
+        SDL_SendAppEvent(SDL_APP_WILLENTERBACKGROUND);
+        SDL_SendAppEvent(SDL_APP_DIDENTERBACKGROUND);
+    
+        /* *After* sending the relevant events, signal the pause semaphore 
+         * so the event loop knows to pause and (optionally) block itself */
+        if (!SDL_SemValue(Android_PauseSem)) SDL_SemPost(Android_PauseSem);
     }
-
-    __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "nativePause()");
-    SDL_SendAppEvent(SDL_APP_WILLENTERBACKGROUND);
-    SDL_SendAppEvent(SDL_APP_DIDENTERBACKGROUND);
 }
 
 /* Resume */
@@ -317,8 +318,6 @@
                                     JNIEnv* env, jclass cls)
 {
     __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "nativeResume()");
-    SDL_SendAppEvent(SDL_APP_WILLENTERFOREGROUND);
-    SDL_SendAppEvent(SDL_APP_DIDENTERFOREGROUND);
 
     if (Android_Window) {
         /* Signal the resume semaphore so the event loop knows to resume and restore the GL Context
@@ -326,6 +325,9 @@
          * and this function will be called from the Java thread instead.
          */
         if (!SDL_SemValue(Android_ResumeSem)) SDL_SemPost(Android_ResumeSem);
+        
+        SDL_SendAppEvent(SDL_APP_WILLENTERFOREGROUND);
+        SDL_SendAppEvent(SDL_APP_DIDENTERFOREGROUND);
         SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_FOCUS_GAINED, 0, 0);
         SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_RESTORED, 0, 0);
     }
--- a/src/video/android/SDL_androidevents.c	Thu Nov 28 02:31:32 2013 -0800
+++ b/src/video/android/SDL_androidevents.c	Fri Nov 29 10:06:08 2013 -0300
@@ -66,8 +66,6 @@
     /*
      * Android_ResumeSem and Android_PauseSem are signaled from Java_org_libsdl_app_SDLActivity_nativePause and Java_org_libsdl_app_SDLActivity_nativeResume
      * When the pause semaphore is signaled, if SDL_ANDROID_BLOCK_ON_PAUSE is defined the event loop will block until the resume signal is emitted.
-     * When the resume semaphore is signaled, SDL_GL_CreateContext is called which in turn calls Java code
-     * SDLActivity::createGLContext -> SDLActivity:: initEGL -> SDLActivity::createEGLSurface -> SDLActivity::createEGLContext
      */
 
 #if SDL_ANDROID_BLOCK_ON_PAUSE