Running Android games in background throws errors each frame :"Skipped rendering frame because GfxDevice is in invalid state (device lost)"

We managed to make Unity run from a class that extends UnityNativePlayerActivity, got it to make and show Toasts on window change, to be sure that it is actually set up properly.

Updating Unity3D frame by frame doesn’t work entirely while in background mode( it works as intended when the game is focused), the logging console shows me that it tries to, but it will skip the current frame because the Unity window itself isn’t focused. Some parts of the code seem to be working, but for one, the audio system doesn’t work at all. The console throws those errors every single update:

Are there any solutions that anyone has encountered related to my problem? Is it even possible to run a Unity game in background on Android?

E/Unity   ( 4285): [EGL] Failed to create surface
E/Unity   ( 4285):  
E/Unity   ( 4285): (Filename:  Line: 132)
E/Unity   ( 4285): 
E/Unity   ( 4285): [EGL] Error:: EGL_BAD_NATIVE_WINDOW: A NativeWindowType argument does not refer to a valid native window.
E/Unity   ( 4285):  
E/Unity   ( 4285): (Filename: ./PlatformDependent/AndroidPlayer/ContextGLES.cpp Line: 132)
E/Unity   ( 4285): 
I/Unity   ( 4285): Skipped rendering frame because GfxDevice is in invalid state (device lost)
I/Unity   ( 4285):  
I/Unity   ( 4285): (Filename:  Line: 1539)

Thanks in advance for the help.

It does not work. The log output tells you why (basically no active native window available).

Unity runs as Activity in Android as you know, hence it gets paused when it’s loses it’s focus. That’s by (Android) Design and works as intended.

The only way to run stuff in background on Android is through a Service. A service however, can not have visible parts, therefore, running a game with unity as a service is impossible.

Of course it depends on the kind of app/game you want to make, if a service is usable or not.