c++ global variables persist across runs on Android

When the main function returns, SDL doesn’t kill the process. You can still see your app with black screen in the window selector. When the user opens the app again from the launcher, SDL will invoke SDL_main again, but without re-initializing globals. In my case it will immediately quit, as the global variable quit is already true. I can only open my app again after tapping “Force Stop” in Android.
The problem was solved, by adding exit(0) at the end, but the app still remains visible in the window selector.
I believe this is a bug and a risk for new SDL users.

global variable: bool quit = false;

void SDL_main() {
while(!quit)
{ … }
SDL_Quit();
}

1 Like

It’s not a bug. I believe this is unavoidable on Android due to how the spaghetti is cooked in the background, so you have to just work around it. It’s inconvenient, but you need to make sure you refresh all your global data after main() instead of initialising it in the global space. I think I remember reading a note about this in the android documents.

@archee303
I don’t think you should observe this behavior.
how is you AndroidManifest.xml ?
so you have "android:launchMode=“singleInstance” ?