SDL: Invalid Focus Event Sequence on Android

I recently released an SDL-based mobile game on iOS and Android called Trippin on Tubs.

That game is built atop SDL 2.26.5.

Overall, it has been completely smooth release process. SDL is an amazing foundation! So thank you, SDL contributors.

However, I noticed something odd on some Android devices, including my Pixel 6 Pro. After the app launches, when I tap the screen, a focus-lost event SDL_WINDOWEVENT_FOCUS_LOST is received in the event loop. That triggers application logic to pause gameplay. A subsequent interaction triggers SDL_WINDOWEVENT_FOCUS_GAINED and things proceed normally.

Bizarrely, I do not encounter this when I run debug builds via Android Studio. I only encounter this in release versions of the application downloaded from Google Play. Perhaps there is a subtle lifecycle difference through the Android OS.

Here’s a log trace the demonstrates the issue (logging modeled after snippet here https://wiki.libsdl.org/SDL2/SDL_WindowEvent):

---------------------------- PROCESS STARTED (11701) for package com.trippinontubs ----------------------------
2023-10-08 10:46:05.520 11701-11737 SDL/APP                 com.trippinontubs                    I  window entered, window=1
2023-10-08 10:46:05.520 11701-11737 SDL/APP                 com.trippinontubs                    I  window gained keyboard focus, window=1
2023-10-08 10:46:05.520 11701-11737 SDL/APP                 com.trippinontubs                    I  window size changed, window=1
2023-10-08 10:46:05.520 11701-11737 SDL/APP                 com.trippinontubs                    I  window resized, window=1
2023-10-08 10:46:05.523 11701-11737 BLASTBufferQueue        com.trippinontubs                    E  [SurfaceView[com.trippinontubs/org.libsdl.app.SDLActivity]#1](f:0,a:0) rejecting buffer:active_size=3120x1440, requested_size=3120x1440 buffer{size=1258x2975 transform=7}
2023-10-08 10:46:06.768 11701-11701 SDL                     com.trippinontubs                    V  onWindowFocusChanged(): false
2023-10-08 10:46:06.768 11701-11701 SDL                     com.trippinontubs                    V  nativeFocusChanged()
2023-10-08 10:46:06.772 11701-11737 SDL/APP                 com.trippinontubs                    I  window lost keyboard focus, window=1
2023-10-08 10:46:10.740 11701-11701 SDL                     com.trippinontubs                    V  onWindowFocusChanged(): true
2023-10-08 10:46:10.740 11701-11701 SDL                     com.trippinontubs                    V  nativeFocusChanged()
2023-10-08 10:46:10.744 11701-11737 SDL/APP                 com.trippinontubs                    I  window gained keyboard focus, window=1
2023-10-08 10:46:12.846 11701-11701 SDL                     com.trippinontubs                    V  onWindowFocusChanged(): false
2023-10-08 10:46:12.846 11701-11701 SDL                     com.trippinontubs                    V  nativeFocusChanged()
2023-10-08 10:46:12.847 11701-11737 SDL/APP                 com.trippinontubs                    I  window lost keyboard focus, window=1
2023-10-08 10:46:13.981 11701-11701 SDL                     com.trippinontubs                    V  onPause()
2023-10-08 10:46:13.990 11701-11701 SDL                     com.trippinontubs                    V  surfaceDestroyed()
2023-10-08 10:46:13.990 11701-11701 SDL                     com.trippinontubs                    V  nativePause()
---------------------------- PROCESS ENDED (11701) for package com.trippinontubs ----------------------------
  • 2023-10-08 10:46:05.520: app launch
  • 2023-10-08 10:46:06.768: finger touch triggered focus loss
  • 2023-10-08 10:46:10.740: finger touch triggered focus gain
  • 2023-10-08 10:46:12.846: closed app

Do you know what might be happening?

Just as important, can you think of a way to debug this more effectively?

About this issue

  • Original URL
  • State: closed
  • Created 9 months ago
  • Comments: 29 (16 by maintainers)

Most upvoted comments

@AntTheAlchemist , @1bsyl it happens with the PowBall game as well! It is not just Trippin on Tubs. Exactly the same pause and resume behavior.

I have not tried your game yet but I intend to today.

That’s great information about hints and events. Maybe I can handle events slightly differently and sidestep the issue that way.

If you add the game tag to your manifest (which you should do anyway), you may be able to debug the issue because it may trigger the overlay of it knows it’s a game outside of Play Store.

The Java-side SDL code does some magic and tweaking to get a drawable layer. It might be what’s upsetting an overlay? I just played with the “Gaming Mode” option on my Modded Nokia 8, which overlays a little menu. It’s not causing a problem until I double-tap to bring up the menu, then I get a focus lost.

Can you try my “Game Controller Tester” app on Google Play? It’s built with SDL3, but should still show up if this is a wider issue or not. My app has an option in settings (enable expert mode) that will display SDL “System Events”. You’ll see focus gained / lost events in real time. See if this game pad overlay is producing the focus lost event in my app. Keep in mind that most event names have been renamed in SDL3, but you’ll figure out what they compared in SDL2.

Make sure you halt all rendering if you get a SDL_APP_WILLENTERBACKGROUND event. Resume when you get a SDL_APP_DIDENTERFOREGROUND. Probably won’t make a difference.

Just fyi, I’ve been launching apps on Android with SDL for a few years now and haven’t experienced stray SDL_WINDOWEVENT_FOCUS_LOST events. I’m using the latest SDL3 at the moment and can’t reproduce it, even from Play Store apps.

What hints are you using? Anything like the ones that convert touch events to mouse or visa versa? Must be something unique to your build. Can you reproduce it be side-loading the release APK?