Repository: kivy/kivy

Issue:
android: [WindowSDL] No running App found, exit.
Sometimes android app is being paused  during initialization, before the kivy.app.App instance is created. In such case it exits with error `[WindowSDL] No running App found, exit.`

It happens with some apps, when `--window` arg is passed to p4a.
Essentially what happens is: 
1) SDL2 Window is created
2) App paused
3) kivy tries to find App instance to handle pause, and fails
4) stopTouchApp() called from window_sdl2.py `do_pause()`
5) App instance is created
6) stopTouchApp() forces app to exit.

Log:
```
06-25 19:27:20.316 I/python  (27304): [INFO   ] [Window      ] Provider: sdl2
06-25 19:27:20.346 V/SDL     (27304): setOrientation() orientation=-1 width=800 height=600 resizable=true hint=
06-25 19:27:20.356 I/python  (27304): [INFO   ] [GL          ] Using the "OpenGL ES 2" graphics system
06-25 19:27:20.356 I/python  (27304): [INFO   ] [GL          ] Backend used <sdl2>
06-25 19:27:20.356 I/python  (27304): [INFO   ] [GL          ] OpenGL version <b'OpenGL ES 3.1 V@124.0 (GIT@I041b18199e)'>
06-25 19:27:20.366 V/SDL     (27304): surfaceDestroyed()
06-25 19:27:20.366 V/SDL     (27304): nativePause()
06-25 19:27:20.396 I/python  (27304): [INFO   ] [WindowSDL   ] No running App found, exit.
06-25 19:27:20.406 V/SDL     (27304): surfaceCreated()
06-25 19:27:20.406 V/SDL     (27304): surfaceChanged()
06-25 19:27:20.406 V/SDL     (27304): pixel format RGBA_8888
06-25 19:27:20.406 I/python  (27304): [INFO   ] [Window      ] auto add sdl2 input provider
06-25 19:27:20.406 V/SDL     (27304): Window size: 1080x1845
06-25 19:27:20.406 V/SDL     (27304): Device size: 1080x1920
06-25 19:27:20.406 I/python  (27304): [INFO   ] [Window      ] virtual keyboard not allowed, single mode, not docked
06-25 19:27:20.406 V/SDL     (27304): nativeResume()
06-25 19:27:20.576 I/python  (27304): [INFO   ] [Text        ] Provider: sdl2
06-25 19:27:23.186 I/python  (27304): [INFO   ] [GL          ] NPOT texture support is available
06-25 19:27:23.466 I/python  (27304): [WARNING] [Lang        ] constants.kv has already been included!
06-25 19:27:24.386 I/python  (27304):  2019-06-25 19:27:24.396 | INFO     | __main__:build:107 - Build started
06-25 19:27:24.416 I/python  (27304): [WARNING] [Base        ] Unknown <android> provider
06-25 19:27:24.416 I/python  (27304): [INFO   ] [Base        ] Start application main loop
06-25 19:27:24.426 I/python  (27304): [INFO   ] [Base        ] Leaving application in progress...
06-25 19:27:24.796 I/python  (27304): [INFO   ] [WindowSDL   ] exiting mainloop and closing.
06-25 19:27:24.926 I/python  (27304): Python for android ended.
```


Discussion:

Resolving patch:
diff --git a/kivy/core/window/window_sdl2.py b/kivy/core/window/window_sdl2.py
--- a/kivy/core/window/window_sdl2.py
+++ b/kivy/core/window/window_sdl2.py
@@ -229,11 +229,9 @@ def _event_filter(self, action, *largs):
             from kivy.base import stopTouchApp
             app = App.get_running_app()
             if not app:
-                Logger.info('WindowSDL: No running App found, exit.')
-                stopTouchApp()
-                return 0
+                Logger.info('WindowSDL: No running App found, pause.')
 
-            if not app.dispatch('on_pause'):
+            elif not app.dispatch('on_pause'):
                 Logger.info(
                     'WindowSDL: App doesn\'t support pause mode, stop.')
                 stopTouchApp()
@@ -248,7 +246,8 @@ def _event_filter(self, action, *largs):
             if self._pause_loop:
                 self._pause_loop = False
                 app = App.get_running_app()
-                app.dispatch('on_resume')
+                if app:
+                    app.dispatch('on_resume')
 
         elif action == 'windowresized':
             self._size = largs
@@ -707,11 +706,8 @@ def do_pause(self):
         from kivy.base import stopTouchApp
         app = App.get_running_app()
         if not app:
-            Logger.info('WindowSDL: No running App found, exit.')
-            stopTouchApp()
-            return
-
-        if not app.dispatch('on_pause'):
+            Logger.info('WindowSDL: No running App found, pause.')
+        elif not app.dispatch('on_pause'):
             Logger.info('WindowSDL: App doesn\'t support pause mode, stop.')
             stopTouchApp()
             return
@@ -732,8 +728,9 @@ def do_pause(self):
                 break
             elif action == 'windowrestored':
                 break
-
-        app.dispatch('on_resume')
+            
+        if app:
+            app.dispatch('on_resume')
 
     def _update_modifiers(self, mods=None, key=None):
         if mods is None and key is None:


Test patch: