Android black screen/assert when returning from background

hi.
im using sdl for the first time.
im getting a black screen/assert when the app come back from background on galaxy 9 sm-g965f android 8.0.0 sdl version 2.0.9

any idea how to solve it?

EDIT: sdl_geterror() return unable to show color buffer in an OS-native window (call to eglSwapBuffers failed, reporting an error of EGL_BAD_SURFACE)
from SDL_egl.c at SDL_EGL_SwapBuffers

this is the main.cpp

#include "SDL.h"
#include "SDL_image.h"

static int bkgnd = 0;

int events(void* userdata, SDL_Event* eve)
{
switch (eve->type)
{
case SDL_APP_WILLENTERBACKGROUND:
	bkgnd = 1;
	break;
case SDL_APP_WILLENTERFOREGROUND:
	bkgnd = 0;
	break;
}
return 0;
}

int main(int argc, char *argv[])
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window* window = SDL_CreateWindow("game name", 0, 0, 400, 400, SDL_WINDOW_SHOWN | SDL_WINDOW_FULLSCREEN);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);//SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);

SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);

SDL_AddEventWatch(events, 0);

int a = 0;
while (a < 1000)
{
	SDL_Delay(10);
	a++;

	if (bkgnd == 0) {
		SDL_LogMessage(SDL_LOG_CATEGORY_TEST, SDL_LOG_PRIORITY_DEBUG, "ttttest render");

		renderer = SDL_GetRenderer(window);

		SDL_RenderClear(renderer);
		SDL_SetRenderDrawColor(renderer, 255, a % 255, 255, 255);

		//SDL_Surface *tempSurface = IMG_Load("1.png");
		//SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, tempSurface);
		//SDL_FreeSurface(tempSurface);

		//SDL_RenderCopy(renderer, texture, NULL, NULL);

		SDL_RenderPresent(renderer);
	}
}

SDL_LogMessage(SDL_LOG_CATEGORY_TEST, SDL_LOG_PRIORITY_DEBUG, "ttttest finish");

return 0;
}

this is the logcat output when minimizing the app:

D/SDL/(null): ttttest render
D/SDL/(null): ttttest render
D/SDL/(null): ttttest render
E/libEGL: call to OpenGL ES API with no current context (logged once per thread)
D/SensorManager: unregisterListener :: 1, LSM6DSL Acceleration Sensor,  
D/ViewRootImpl@b0fb8f6[SDLActivity]: MSG_WINDOW_FOCUS_CHANGED 0
V/SDL: onWindowFocusChanged(): false
D/SurfaceView: BG show() Surface(name=Background for - SurfaceView - org.libsdl.app/org.libsdl.app.SDLActivity@8f66e82@0) org.libsdl.app.SDLSurface{8f66e82 VFE...... .F...... 0,0-720,1280}
D/SurfaceView: surfaceDestroyed 1 org.libsdl.app.SDLSurface{8f66e82 VFE...... .F...... 0,0-720,1280}
V/SDL: surfaceDestroyed()
D/SurfaceView: BG show() Surface(name=Background for - SurfaceView - org.libsdl.app/org.libsdl.app.SDLActivity@8f66e82@0) org.libsdl.app.SDLSurface{8f66e82 VFE...... .F...... 0,0-720,1280}
D/OpenGLRenderer: eglDestroySurface = 0xd87f0368
D/ViewRootImpl@b0fb8f6[SDLActivity]: Relayout returned: old=[0,0][720,1280] new=[0,0][720,1280] result=0x5 surface={valid=false 0} changed=true
D/ViewRootImpl@b0fb8f6[SDLActivity]: Relayout returned: old=[0,0][720,1280] new=[0,0][720,1280] result=0x1 surface={valid=false 0} changed=false
D/SDL/(null): ttttest finish
V/SDL: Finished main function
V/SDL: onDestroy()
E/BpSurfaceComposerClient: Failed to transact (-1)
D/SurfaceView: BG destroy() Surface(name=Background for - SurfaceView - org.libsdl.app/org.libsdl.app.SDLActivity@8f66e82@0) org.libsdl.app.SDLSurface{8f66e82 VFE...... .F...... 0,0-720,1280}
E/BpSurfaceComposerClient: Failed to transact (-1)
D/ViewRootImpl@b0fb8f6[SDLActivity]: dispatchDetachedFromWindow
D/InputEventReceiver: channel 'c884fc org.libsdl.app/org.libsdl.app.SDLActivity (client)' ~ Disposing input event receiver.
channel 'c884fc org.libsdl.app/org.libsdl.app.SDLActivity (client)' ~NativeInputEventReceiver.
V/SDL: Device: star2lte
V/SDL: Model: SM-G965F
onCreate()
V/SDL: nativeSetupJNI()
AUDIO nativeSetupJNI()
CONTROLLER nativeSetupJNI()
D/hidapi: Initializing Bluetooth
Couldn't initialize Bluetooth, missing android.permission.BLUETOOTH
V/SDL: onResume()
D/ViewRootImpl@9874d83[SDLActivity]: setView = DecorView@e8c1b00[SDLActivity] TM=true MM=false
V/InputMethodManager: Not IME target window, ignoring
D/ViewRootImpl@9874d83[SDLActivity]: dispatchAttachedToWindow
V/Surface: sf_framedrop debug : 0x4f4c, game : false, logging : 0
D/ViewRootImpl@9874d83[SDLActivity]: Relayout returned: old=[0,0][0,0] new=[0,0][720,1280] result=0x7 surface={valid=true 3632119808} changed=true
D/mali_winsys: EGLint new_window_surface(egl_winsys_display *, void *, EGLSurface, EGLConfig, egl_winsys_surface **, egl_color_buffer_format *, EGLBoolean) returns 0x3000,  [720x1280]-format:1
D/OpenGLRenderer: eglCreateWindowSurface = 0xd87f0688
D/SurfaceView: BG show() Surface(name=Background for - SurfaceView - org.libsdl.app/org.libsdl.app.SDLActivity@3e1af39@0) org.libsdl.app.SDLSurface{3e1af39 VFE...... .F....ID 0,0-720,1280}
V/Surface: sf_framedrop debug : 0x4f4c, game : false, logging : 0
D/SurfaceView: surfaceCreated 1 org.libsdl.app.SDLSurface{3e1af39 VFE...... .F....ID 0,0-720,1280}
V/SDL: surfaceCreated()
D/SurfaceView: surfaceChanged (720,1280) 1 org.libsdl.app.SDLSurface{3e1af39 VFE...... .F....ID 0,0-720,1280}
V/SDL: surfaceChanged()
pixel format RGB_565
Window size: 720x1280
Device size: 720x1280
D/mali_winsys: EGLint new_window_surface(egl_winsys_display *, void *, EGLSurface, EGLConfig, egl_winsys_surface **, egl_color_buffer_format *, EGLBoolean) returns 0x3000,  [720x1280]-format:4
D/SensorManager: registerListener :: 1, LSM6DSL Acceleration Sensor, 20000, 0,  
V/SDL: nativeResume()
V/SDL: Running main function SDL_main from library /data/app/org.libsdl.app-x2tscdjfCzudSfU_DY_y9A==/lib/arm/libmain.so
nativeRunMain()
D/SensorManager: registerListener fail :: 1, LSM6DSL Acceleration Sensor, 20000, 0,  
W/SDL/SYSTEM: Assertion failure at SDL_SetRenderDrawColor_REAL (C:/engine/android/android-project/app/jni/SDL/src/render/SDL_render.c:1550), triggered 1 time:
  'renderer && renderer->magic == &renderer_magic'
D/ViewRootImpl@9874d83[SDLActivity]: MSG_RESIZED_REPORT: frame=Rect(0, 0 - 720, 1280) ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1
MSG_WINDOW_FOCUS_CHANGED 1
V/SDL: onWindowFocusChanged(): true
V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@991297e nm : org.libsdl.app ic=null
I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
D/ViewRootImpl@9874d83[SDLActivity]: Relayout returned: old=[0,0][720,1280] new=[0,0][720,1280] result=0x3 surface={valid=true 3632119808} changed=false
D/ViewRootImpl@9874d83[SDLActivity]: Relayout returned: old=[0,0][720,1280] new=[0,0][720,1280] result=0x1 surface={valid=true 3632119808} changed=false
I/zygote: Do partial code cache collection, code=30KB, data=27KB
I/zygote: After code cache collection, code=30KB, data=27KB
Increasing code cache capacity to 128KB
D/ViewRootImpl@914fead[Assertion Failed]: setView = DecorView@a5755e2[Assertion Failed] TM=true MM=false
D/ViewRootImpl@914fead[Assertion Failed]: dispatchAttachedToWindow
V/Surface: sf_framedrop debug : 0x4f4c, game : false, logging : 0
D/ViewRootImpl@914fead[Assertion Failed]: Relayout returned: old=[0,0][0,0] new=[80,375][640,905] result=0x7 surface={valid=true 3592466432} changed=true
D/mali_winsys: EGLint new_window_surface(egl_winsys_display *, void *, EGLSurface, EGLConfig, egl_winsys_surface **, egl_color_buffer_format *, EGLBoolean) returns 0x3000,  [560x530]-format:1
D/OpenGLRenderer: eglCreateWindowSurface = 0xd87f10b0
D/ViewRootImpl@914fead[Assertion Failed]: MSG_WINDOW_FOCUS_CHANGED 1
V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@2269d73 nm : org.libsdl.app ic=null
I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
D/ViewRootImpl@914fead[Assertion Failed]: MSG_RESIZED_REPORT: frame=Rect(80, 375 - 640, 905) ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1
D/ViewRootImpl@9874d83[SDLActivity]: MSG_WINDOW_FOCUS_CHANGED 0
V/SDL: onWindowFocusChanged(): false
nativePause()
V/SDL: nativePause()
D/SensorManager: unregisterListener :: 1, LSM6DSL Acceleration Sensor,  
D/ViewRootImpl@9874d83[SDLActivity]: Relayout returned: old=[0,0][720,1280] new=[0,0][720,1280] result=0x1 surface={valid=true 3632119808} changed=false

I think you need to call either SDL_PollEvent() or SDL_PumpEvents() somewhere in your main loop.

you are right :slight_smile:

added :
SDL_Event event;
SDL_PollEvent(&event);

solved the issue

You can process all the events in a PollEvent loop (which should repeat until the event queue returns empty), instead of adding the event watch. There’s a chance the watch is called from a different thread and you may need to do something on the main thread.