How to end SDLActivity correctly |
How to end SDLActivity correctly |
hardcoredaniel
Guest
|
Hi,
Android apps are not automatically unloaded even after the onDestroyed() callback, e.g. when they are in the destroyed lifecycle state. They will only be removed from memory if more recently started apps would require memory themselfes. This is probably intentional to have a faster re-start of apps. As a consequence, all code needs to be "re-startable", e.g. not relying on static initialization which is only executed when the code/the library is initially loaded into memory. (There was also a posting to this list where a method is described that removes this requirement from native code, but it means more effort to place all your code into an additional native library which you unload upon onDestroy()) I guess that in your case the app successfully enters the "destroyed" state, but upon restart sth. goes wrong because some code does not work when being called a 2nd time. You should see that in the stacktrace when your app crashes. Regards, Daniel ---------- Původnà zpráva ---------- Od: Edition Chamäleon Komu: Datum: 14. 12. 2016 16:46:35 Předmět: [SDL] How to end SDLActivity correctly
|
|||||||||||||
|
Edition Chamäleon
|
I'd like to add some logcat:
on 03.004 my native is ending "int main" and SDLActivity jumps in "handleNativeExit()" (sounds and feels right) but on 03.014 it jumps in "onPause()" (why is that?) on 03.404 the app is seemingly closed ( but as I wrote it's only seemingly = the app is still standby ) using homebutton to see standby apps and so on .. is all after 03.404
|
|||||||||||||
|
Edition Chamäleon
|
Hi Daniel,
thank you for your fast answer. Resuming my app, when it is paused via homebutton works fine. But the exitbutton that I want to implement should kill the app complete and shut it down. As I wrote I'm killing all of my native app, when the exitbutton is triggered by the user, therefore it isn't restartable anymore. Is that a wrong way? Michael |
|||||||||||
|
How to end SDLActivity correctly |
hardcoredaniel
Guest
|
You cannot completely "exit" an Activity on Android (like on Windows or other OSes where exit() resp. return from main unloads the program from memory). Android will decide for itself whether and when to remove an Activity from memory. Think of it like files that an OS will cache in memory for performance reasons - Android does the same with Activities.
An Activity being in the "destroyed" state should however not behave differently when started, compared to an Activity that has not been started before. And yes, Android apps should always be restartable, because you'll never know whether the app was removed from memory in the meantime. This might need special attention (like no static initialization in native code, and SDLActivity re-initializing some fields upon onDestroy() already, which looks strange but is correct). ---------- Původnà zpráva ---------- Od: Edition Chamäleon Komu: Datum: 14. 12. 2016 17:15:31 Předmět: Re: [SDL] How to end SDLActivity correctly
|
|||||||||||||
|
How to end SDLActivity correctly |
SiPlus
|
You can use exit() the same way as you use it on other OSes, and it will kill all activities and services currently running in the calling process (by default, there is one process for all activities and services, but using the android:process attribute in AndroidManifest.xml you can put different components of your application into different processes - but you won't need it most of the time).
If you're making a game with the traditional singleton lifecycle, it's perfectly fine to use exit() and other per-process things such as global variables, while using the singleTask launch mode to maintain only one activity for your game (the activity basically being the drawing surface and input interface). It's simply not possible to respect the Android lifecycle with everything being local to activity and saved/restored during configuration changes (mostly you can even skip configuration changes at all using the android:configChanges attribute, and/or only handle special cases such as font size changes) when you have a game any more complex than Pong or whatever anyway. It's just not how you should design regular apps. Games rarely need the activity-based interaction model, so the singleton architecture is both acceptable and a common practice. SDL is designed in the way that allows having multiple activities and services of the same application running simultaneously, so it doesn't kill the process by itself, but rather notifies the application code that an exit was performed. So it's the application's responsibility to handle the quit event as it needs it to be handled - and if you need to kill the whole process, nothing can stop you from killing the whole process. On 14/12/2016 20:28, hardcoredaniel wrote:
|
|||||||||||||||||
|
EternalLands
|
I always use exit(1); when I want to terminate the app. It works quite well.
Return after the main loop doesn't work well, sometimes it will crash the next time the app is started. |
|||||||||||
|