SDL 2.0.7. Close app on Android

What is the correct way to close/exit app from Android?
For example, when user press back button.

In my case SQL_quit does not finish app. It seem to be it ony minimizes and process still works.

SDL_Quit never did terminate the program, it just posts a quit event to the SDL event queue.

So I need manually execute anything through JNI? Or JVM should catch it itself by this code?

                    // Set up a listener thread to catch when the native thread ends
                mSDLThread = new Thread(new Runnable() {
                    public void run() {
                        try {
                        } catch (Exception e) {
                            // Ignore any exception
                        } finally {
                            // Native thread has finished
                            if (!mExitCalledFromJava) {
                }, "SDLThreadListener");

Last time I made an Android application using SDL, I used the SDL_SCANCODE_AC_BACK scancode to check if the back key on my mobile device was pressed.
So in my event code, I checked for a SDL_KEYDOWN event, then checked if MyEvent.key.keysym.sym was of type SDL_SCANCODE_AC_BACK. If that was the case, I made sure that my application was shutdown by ending the c++ code’s mainloop and destroyed everything, and also executed SDL_Quit();.
By doing this, I think my application was shutdown properly and wasn’t minimized to the background. I might be wrong about this though.

Naith, handling event with scancode is not a problem. By pressing it I exit from main loop and exec


and I see these lines on android log through adb logcat

V/SDL     (15630): Finished main function
V/SDL     (15630): onWindowFocusChanged(): false
V/SDL     (15630): nativePause()
V/SDL     (15630): onPause()
V/SDL     (15630): surfaceDestroyed()
V/SDL     (15630): onDestroy()

But App not finishes, it only minimize and I see it as active by holding home button.

Oh, okay. Can’t help you there, sorry.
Hopefully someone has an answer on this since it might be helpful to people, me included.

There was 4 years ago discussion I have read
But I still don’t understand what is the correct way to close app on Android.

Sorry, I was actually thinking of the Win32 function PostQuitMessage. SDL_Quit shuts down the native code, but the java code is not shut down because it is what calls SDL_main. Android likes to keep processes running in the background for apps that have already quit to reduce startup time when running the app again. If you need to ensure that the process terminates, you can call exit but you will need to save any state you want to keep first.

Thank you. I will try call exit on onDestroy Java method.

My understanding is that apps should never ‘close’, as such. The user can ‘force close’ them manually but they should not do so themselves. I have a commercial Android app (a CCTV viewer) that does include a ‘quit’ feature but if I use that it still appears in the list of backgrounded apps.


rtrussell, yeah, now I think so too. I was never think about it before but yesterday I tested some aps like Google Play and its behavior as you said.

Thanks for all!

On IOS, apps should never exit/quit, but on Android you can.

You simply return from your main() function. Then the Java side will catch that the SDL thread has finished and it will quit the Activity but calling Activity.finish().
You should not call exit() as this stop all the activity I think.

But also, Android can decide to terminate your app by calling onDestroy() (see Activity life cycle), then your app will receive a SDL_QUIT event that you have to handle.