![]() |
SDL2 Android - Exiting | ![]() |
Alvin Beach
Guest
![]() |
![]() |
Hello,
How do you quit/exit the Android application from within C++ (e.g. libmain.so)? I feel this is a silly question, however, when my "int main(int argc, char *argv[])" returns, the app remains running. The README-android.txt doesn't say. AFAIK, Android handles actually terminating the app. In my case, I would like to force quit the app. Alvin _______________________________________________ SDL mailing list http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org |
||||||||||
|
![]() |
SDL2 Android - Exiting | ![]() |
gabomdq
![]() |
![]() |
IIRC issuing "exit" does the trick somewhat forcefully...
Returning from main won't close your app because there's two threads active, the Java side thread and the native thread where your main function runs. Currently, exiting from the later won't exit your app as the Java thread keeps running (until it crashes or something ![]() 2013/11/22 Alvin Beach
-- Gabriel. |
||||||||||||
|
![]() |
SDL2 Android - Exiting | ![]() |
Stefanos A.
Guest
![]() |
![]() |
Doesn't SDL_Quit() do the trick?
2013/11/22 Gabriel Jacobo
|
||||||||||||||
|
![]() |
SDL2 Android - Exiting | ![]() |
Alvin Beach
Guest
![]() |
![]() |
On 22/11/13 13:19, Gabriel Jacobo wrote:
Thank you, that did the trick. I should have tried that before posting! Normally, I tend to use _exit() when I know there is nothing left to clean up (no open files, etc.). IIRC, exit() will does some clean up operations whereas _exit() just terminates right then an there. Do you know of any reason why exit() would be better to use on Android/SDL2 rather than _exit()? Thanks, Alvin _______________________________________________ SDL mailing list http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org |
||||||||||||
|
![]() |
SDL2 Android - Exiting | ![]() |
Alvin Beach
Guest
![]() |
![]() |
On 22/11/13 13:28, Stefanos A. wrote:
I thought that would have worked too. It just shuts down SDL not the app. What becomes of the app after calling SDL_Quit(), I'm not sure. The use case for calling SDL_Quit and not really wanting the app to exit (SDL2 app I mean), isn't clear to me. Alvin _______________________________________________ SDL mailing list http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org |
||||||||||||
|
![]() |
SDL2 Android - Exiting | ![]() |
Stefanos A.
Guest
![]() |
![]() |
The Java code calls nativeQuit in the onDestroy handler. I can't find any relevant for the opposite direction, i.e. a check that mSDLThread has quit, which explains why the activity keeps running.
I'm not sure if there is any straightforward way to check that, someone more familiar with Android should pitch in. 2013/11/22 Alvin Beach
|
||||||||||||||||||
|
![]() |
SDL2 Android - Exiting | ![]() |
Jared Maddox
Guest
![]() |
![]() |
I think this is a somewhat silly idea, but you might be using SDL as a wizard, and then doing all further work non-interactively. You can say "but Android expects your app to have a window!", but it isn't really relevant, since desktops are supported too. At any rate, having SDL_Quit() actually exit the application would be a BAD idea. If you're only using one library, then sure, it probably wouldn't bite you, but if you're using multiple libraries, where for each one it doesn't make sense to continue running the program after it's deinitialized, then you start running into questions of what happens if more than one expects to close the program. In essence, for libraries to play nicely together, you need to NOT do this in any function that DOESN'T have this as it's ONLY role: and SDL_Quit() wouldn't have this as it's only role, since it's the function that is used to deinitialize the entire library. In short, SDL_Quit() calling exit() or doing anything else of that sort is an inherently bad idea. _______________________________________________ SDL mailing list http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org |
||||||||||||||
|
![]() |
SDL2 Android - Exiting | ![]() |
Alvin Beach
Guest
![]() |
![]() |
On 22/11/13 23:29, Jared Maddox wrote:
I agree, SDL_Quit() should _not_ call exit() for all the reasons you listed above. Since SDL_Init() has to be called by the C++ code, it wouldn't be consistent (at the very least) for SDL_Quit() to exit() the app. My statement about "SDL_Quit() and the state of the app" was concerning the state of the threads. When main() returns, it's thread presumably dies. This leaves the Java thread running. I believe there is no scenario to get main() to start up again. The app needs to be terminated and restarted for main() to run again. I am using the most excellent android-project/ from the SDL2 source as my base. As I understand it, its design is that there are two threads: one for the Java side (the App - the "shim" according to Readme-android.txt) and the other that runs the C++ code (i.e. in libmain.so). My thought process was that, for Android (iOS too perhaps, but I'm Android centric atm), if SDL_Quit() could notify the Java side that the SDL2 infrastructure has shutdown. In this case, the Java side would do a proper shutdown (e.g. finalise(), and destroy the SDLActivity). AFAIK, destroying the SDLActivity would cause the Android OS to terminate the app. Perhaps, SDL_Quit() would be the wrong place for this. Perhaps the Java "shim" could detect that the thread for libmain.so has terminated (or be notified it has been) and then begin terminating itself as well? Again, this would only apply to Android (and possibly iOS). From what I can see in the source is that the Java-side of the SDL2 project properly terminates itself when told to by the OS (i.e. onDestroy() I believe). I am wondering if there is a way to trigger that behaviour from within the C++ code. This all may be for not since an app is not /suppose/ to exit itself. However, main() does eventually end, in which case there is this lingering app with only half of it running? Alvin _______________________________________________ SDL mailing list http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org |
||||||||||||||||
|
![]() |
SDL2 Android - Exiting | ![]() |
Jared Maddox
Guest
![]() |
![]() |
<snip>
Technically, this depends on the code that called main() in the first place, but I agree with you that it's doubtful that such a "reincarnation" technique is incorporated there. Even if it is, presumably anything that fixes the orphaned-thread problem can later be extended to work in that case too.
Maybe there's a way to ask the OS to do the termination? All that's really needed is (at least I hope) to slip the call to main() between the initial setup, and the final tear-down of the C++ thread, while signalling at the end of that thread that the Java thread should exit too. _______________________________________________ SDL mailing list http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org |
||||||||||||||||||
|
![]() |
SDL2 Android - Exiting | ![]() |
hardcoredaniel
Guest
![]() |
![]() |
Hi,
I'm using Activity.finish() to do that. I defined myself a Handler in SDLActivity.java protected Handler exitHandler = new Handler() { public void handleMessage(Message msg) { finish(); } }; and I send a message to that Handler when the SDL C thread terminates. // Runs SDL_main() SDLActivity.nativeInit(); // Log.v("SDL", "SDL thread terminated"); exitHandler.sendEmptyMessage(0); The call to finish() triggers the lifecycle management of the Android system to "tear down" the app (although the virtual machine will not necessarily exit). In the process of that, "onDestroy()" is called and that will nicely terminate everything. My solution is not really elegant, as it requires you to hand the reference to the "exitHandler" all the way to the SDLMain class. But it works :-) Regards, Daniel ---------- Původnà zpráva ---------- Od: Jared Maddox Datum: 24. 11. 2013 Předmět: Re: [SDL] SDL2 Android - Exiting
|
||||||||||||||||||||
|
![]() |
SDL2 Android - Exiting | ![]() |
Martin Gerhardy
Guest
![]() |
![]() |
Hi,
is there anything that speaks against putting this fix upstream? As far as I can tell, this is still an issue. It would at least be cool if there wouldn't be "new SDLMain()" but "protected SDLMain getSDLMain()" in the code. So one could override this in his own Activity class without the need to modify SDLActivity.java (which is always a must-have for me, because I want to be able to patch things easily). Greetings Martin Am 24.11.2013 10:42, schrieb hardcoredaniel:
|
||||||||||||||||||||||||
|
![]() |
SDL2 Android - Exiting | ![]() |
gabomdq
![]() |
![]() |
I think this should do the trick: https://hg.libsdl.org/SDL/rev/a9c5ddad50b0
2013/12/5 Martin Gerhardy
-- Gabriel. |
||||||||||||||||||||||||||||
|
![]() |
SDL2 Android - Exiting | ![]() |
Alvin Beach
Guest
![]() |
![]() |
On 05/12/13 09:53, Gabriel Jacobo wrote:
Hello Gabriel, Your changes work perfectly! I grabbed the latest SDL2 from Mercurial, replaced my app's jni/SDL2, removed my call to _exit() and rebuilt the app. When main() returns, the app exits cleanly. Thank you. Just in case anyone else tries using the latest SDL2 from Mercurial, remember to update your app's src/org/libsdl/app/SDLActivity.java with that from SDL2/android-project/. I forgot and, well, all sorts of chaos ensued! Alvin _______________________________________________ SDL mailing list http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org |
||||||||||||
|
![]() |
Re: SDL2 Android - Exiting | ![]() |
Limanima
![]() |
![]() |
Sorry to bump this old thread again, but I still have issues with app closing on Android. When my main exits (in c side) , the activity ends (onDestroy in SDLActivity runs) but the process is still active. Maybe this doesn't work like I think it should, I don't know. Shouldn't the process terminate when the activity finishes? There's an enigmatic line of code / comment in the onDestroy event in SDLActivity: @Override protected void onDestroy() { code removed for simplicity // Reset everything in case the user re opens the app SDLActivity.initialize(); } Reset everything in case the user re opens the app? Shouldn't the app die at this point? Reopening the app shouldn't be a new process? I'm confused... |
||||||||||||||
|
![]() |
SDL2 Android - Exiting | ![]() |
hardcoredaniel
Guest
![]() |
![]() |
Hi,
Android apps typically do not "exit", i.e. the VM process does not terminate, even after onDestroy(). (Unless the system decides to kill the process, when resources become tight). The code in OnDestroy() makes sure that everything is re-initialized (at the Java side) if (at any later point) onCreate() is called and the VM process has not been terminated in between. As a consequence, C code that is loaded as a library into the VM (like SDL) cannot use static initializers, because the C library is not re-loaded upon OnCreate() if it was already loaded. Regards, Daniel ---------- Původnà zpráva ---------- Od: Limanima Komu: Datum: 27. 1. 2015 12:01:01 Předmět: Re: [SDL] SDL2 Android - Exiting
|
||||||||||||
|
![]() |
Re: SDL2 Android - Exiting | ![]() |
![]() |
![]() |
SiPlus
![]() |
![]() |
What about running SDL activities in separate processes using android:process of <activity> so we can use C exit without killing other activities?
|
||||||||||
|