Ending an application

Monkey Forums/Monkey Programming/Ending an application

therevills(Posted 2011) [#1]
How does one do it? In Android when you quit the application it is actually still running and you need to force quit it...


Canardian(Posted 2011) [#2]
I think this is a quite ugly hack, because I assume that you are not supposed to use the trans module in your games, but it works :)
Import mojo
Import trans.trans	' somekind of hack to get the ExitApp command

Class Game Extends App
	Method OnCreate()
		SetUpdateRate 60
	End
	Method OnRender()
		Cls 30,30,30
		DrawText "PRESS ESC TO EXIT",0,0
		If KeyDown(KEY_ESCAPE) Then ExitApp
	End
End

Function Main()
	New Game
End



ziggy(Posted 2011) [#3]
Maybe the ExitApp command sohuld have to be moved from trans to monkey?

EDIT: I think you can just import trans.system, and call system.ExitApp


Canardian(Posted 2011) [#4]
Yeah, you're right, I think this is how it should be done:
Import mojo
Import trans.system	' this should be the right way to get the ExitApp command

Class Game Extends App
	Method OnCreate()
		SetUpdateRate 60
	End
	Method OnRender()
		Cls 30,30,30
		DrawText "PRESS ESC TO EXIT",0,0
		If KeyDown(KEY_ESCAPE) Then ExitApp
	End
End

Function Main()
	New Game
End



Virtech(Posted 2011) [#5]
Using "Import trans.trans" or "Import trans.system"
Both refuse to compile...

Parsing...
C:/MonkeyPro/modules/trans/system.monkey<9> : Error : File 'C:/MonkeyPro/modules/trans/native/system.java' not found.
Error in compilation!



Canardian(Posted 2011) [#6]
I guess the trans module is meant only for Windows, Linux and MacOS, so it lacks of other target implementations. Although I think it's bad that Monkey uses Java for Android, since I understood it's not needed anymore.


ziggy(Posted 2011) [#7]
I think the trans module is only focused at the strcpp target, wich is meant to be used for command line applications such as the trans compiler. So it seems the ExitApp can't be done just from any other target.


Virtech(Posted 2011) [#8]
Yes compiling for target glfw worked.

Html5 or Android didnt work.


therevills(Posted 2011) [#9]
Really need this for Android!


Brucey(Posted 2011) [#10]
I don't think you want to be in trans at all... it's designed for its own task (of code generation).
I'm sure Mark will add the specific functionality elsewhere, when he realises it is missing :-)


marksibly(Posted 2011) [#11]
Hi,

Yeah, the ExitApp in trans is a stdcpp thing and wont work on other targets. It will probably end up in a 'posix' or 'crt' module at some point.

Exiting an app can actually be quite a tricky issue - how do you 'exit' an html page for example? Close it abruptly (you can't anyway)?

And in general, why should you ever need to exit an app? I quite like the approach more and more cellphone apps/games seem to be taking, where you never really exit them, but continue where you were last at - ie: persistent apps.

So, my current thinking on the ExitApp issue is to treat app exit as a kind of error.

Currently, the Error function should cause an app exit on all platforms. For web based apps, it sets an internal 'dead' flags that prevents any further 'On' handlers being called - ie: it looks like the app has 'exited', but it's still actually running at the Mojo level.

At the moment, Error will attempt to create some kind of requester showing the error + stack trace. But I was thinking of tweaking it so that Error with an empty string prevents this - ie: just exits the app.

So, Error "" would by ExitApp.

It may sound weird to think of exiting an app as being an 'error', but if the only reason you're exiting are because you're worried about memory or resources or the state of your app in general(!) then it kind of is.

Otherwise, why can't your app just run and run, and the *user* can choose to back to the main menu or whatever?

How does this sound?

> Really need this for Android!

Why is this 'really needed'? And why just for Android?!?


therevills(Posted 2011) [#12]
Why is this 'really needed'?


For Android - when you exit via the Home button, the application is still running in the background and the only way to kill it is to Force Stop it via the Settings > Applications > Manage applications menu.

Try my MonkeyPong.apk for example, when you quit, you still can hear the sounds in the background.


marksibly(Posted 2011) [#13]
Hi,

> Try my MonkeyPong.apk for example, when you quit, you still can hear the sounds in the background.

Have you tried the latest version?

It should automatically stop timers/mute sounds etc when app is suspended.

If not - bug!


therevills(Posted 2011) [#14]
Have you tried the latest version?


v31? Then yes! Bug :P


marksibly(Posted 2011) [#15]
Hi,

Ok, but once that's fixed, do you still really need ExitApp?


therevills(Posted 2011) [#16]
Just been testing stuff:

When playing AngryBirds, when you hit the Home button, the game is still running. When you hit the Back button on the Title-screen it asks you if you want to quit and if you click (touch?) the Tick, it totally quits.

So I think we need it to be like this, an app cant just be left running forever...


marksibly(Posted 2011) [#17]
Hi,

> So I think we need it to be like this, an app cant just be left running forever...

I'll add Error "", but I don't think there's any reason an app can't be 'left running forever' - well, logically anyway.

The way I understand it, apps will eventually get 'spilled' from memory when enough are suspended (unless you've got virtual memory in which case you don't even need to spill - but I don't think phones do yet), at which point OnDestroy would get called and the app would effectively restart (resuming from data saved at OnDestroy) next time it was launched. Done this way, there's no need for a 'Quit'.

Of course, I may have massively misunderstood this, but I have noticed that I never seem to have to 'quit' anything on the iPad - and I like it!


Perturbatio(Posted 2011) [#18]
I'll add Error "", but I don't think there's any reason an app can't be 'left running forever' - well, logically anyway.


On Android there are apps to kill apps running in the background because they can slow down the phone. The benchmarking stuff I've seen always recommends killing running apps before testing.


therevills(Posted 2011) [#19]
I think you are correct Mark.

Just been reading this:

http://developer.android.com/guide/topics/fundamentals/activities.html

Which gives the life cycle of an Activity:


Shutting Down an Activity
You can shut down an activity by calling its finish() method. You can also shut down a separate activity that you previously started by calling finishActivity().

Note: In most cases, you should not explicitly finish an activity using these methods. As discussed in the following section about the activity lifecycle, the Android system manages the life of an activity for you, so you do not need to finish your own activities. Calling these methods could adversely affect the expected user experience and should only be used when you absolutely do not want the user to return to this instance of the activity.



With other targets like Windows and Mac I would normally create a GUI button "Quit" on the titlescreen - how do we quit from these targets?

With HTML5 and Flash, its a webpage, so no quitting can really happen.


marksibly(Posted 2011) [#20]
Hi,

> On Android there are apps to kill apps running in the background because they can slow down the phone.

Hopefully not Monkey apps!

I did notice that Android doesn't automatically pause audio when an app is suspended where iOS does, so I suspect iOS is much better behaved in this respect.

> I think you are correct Mark.

I think it's where things are heading in general - as everything becomes more and more web based too, apps will become more 'embedded' (in web pages, browsers etc - think google docs) and quitting just wont be an option.

And with virtual memory, it's not really an issue at all, like in MacOS where you just tend to leave everything running all the time.

But with phones, there's the wrinkle that your app can be killed at any time after 'suspend' (app may not get an OnDestroy - just read up on that) if memory is tight.

So I think Monkey apps will have 2 choices: the old way, where you just stick 'Error ""' in OnSuspend so the app quits when suspended; or the 'modern' way, where you save enough state in OnSuspend to be able to 'fake' OnResume with OnCreate, just in case your app was nuked.

>With other targets like Windows and Mac I would normally create a GUI button "Quit" on the titlescreen - how do we quit from these targets?

Error "" - it's definitely going in.


Canardian(Posted 2011) [#21]
I tried Error "Exit" on Android, but it only pops up a Java dialog window, and when I hit the Android Back button, the app still runs.

Maybe there is some command in Java to crash the whole running Java application, perhaps by doing a division by zero, or poking some memory location to give a general protection fault, or flooding the memory until it crashes.

It's really horrible to have to go through 10 screens to exit a Monkey app on Android.


Virtech(Posted 2011) [#22]
Using Error("") sounds really ugly for something as trivial as closing a cellphone app. But as long as we are able to quit apps with it (if we wish to have this sort of behaviour) I'm fine with it.

I can always do this...

Function ExitApp()
Error("")
End


Edit: Error "" should then only kill the app. No dialog boxes. Just quit please.


Perturbatio(Posted 2011) [#23]
In HTML I don't see the need to actually exit the app, but if you could expose an event to the host page so that people can write javascript to respond to it (perhaps by replacing or hiding the canvas element) then that might be helpful.


Virtech(Posted 2011) [#24]
I agree there is no use on the HTML platform. But on the cellphone platform its imperative IMO.


Canardian(Posted 2011) [#25]
Yay, I found a way to nicely quit your Android apps, no dialog boxes, just a quick and silent exit:
http://www.monkeytools.net/articles/Exiting_a_Monkey_app

Now I'll make a module which works for all targets too.


Virtech(Posted 2011) [#26]
Thanks Lumooja! Works perfectly on Android.

Now we only need to being able to detect keyhit() or keydown() on those android phone buttons "back","home" and "settings"


Canardian(Posted 2011) [#27]
Note: In most cases, you should not explicitly finish an activity using these methods. As discussed in the following section about the activity lifecycle, the Android system manages the life of an activity for you, so you do not need to finish your own activities. Calling these methods could adversely affect the expected user experience and should only be used when you absolutely do not want the user to return to this instance of the activity.

A game is an activity which has a clear beginning and end, so for games an ExitApp method is needed. Infinite activities, like the Android GUI don't need an ExitApp command. Besides, the Android phone has very little RAM, either 512MB (Galaxy S) or 1024MB (Galaxy S2), so every byte counts, and you should be able to close apps which you don't need every day. Prior to the Android 2.2 upgrade, the memory was always full and you needed manually to close apps to have enough memory to start new apps. With Android 2.2 the OS uses now much less memory and you have more of those 512MB RAM free, but it still gets full if you don't close apps which you don't need at the moment. The Galaxy S2 with it's 1024MB and Android 2.3 will make much more sense with infinite activities, but still games are not infinite activities, as they also may use huge amount of RAM and even swap some assets because not even 1024MB is enough for a bigger game.


DruggedBunny(Posted 2011) [#28]
Error "" as a way to exit does seem rather unintuitive to me, particularly on non-phone/web apps (eg. XNA/GLFW Windows targets)! Not the end of the world, though...


John Galt(Posted 2011) [#29]
I think it should be possible to exit an app on platforms where this is supported.


Canardian(Posted 2011) [#30]
I just added html5 ExitApp too, it's kinda cool! :)
Now I'm adding flash ExitApp, but first I need to install the Flash SDK.


Sledge(Posted 2011) [#31]
I just added html5 ExitApp too, it's kinda cool! :)
Really? Because it sounds kinda retarded, in the proper sense of the word. Apps responding to a notification from the system that they are about to be terminated is the progressive way things are done; apps terminating themselves ad hoc = error. Is there seriously no facility in the Android SDK for this?!


Canardian(Posted 2011) [#32]
The html5 ExitApp works quite well. It doesn't close the browser window, because you might be running the app in a iframe or frameset, so it only rewrites the body content, so you can restart the game by refreshing the page. I think I will add also a paramters to the ExitApp function, so you can write your own text or even a redirect with HTML.

ANSI C++ is part of the Android SDK, so the android ExitApp works fine too.


DGuy(Posted 2011) [#33]
Just some food for thought concerning iOS (from Apples' iOS Reference Library):

Technical Q&A QA1561

Q: How do I programmatically quit my iPhone OS application?

A: There is no API provided for gracefully terminating an iPhone application. Under the iPhone OS, the user presses the Home button to close applications. Should your application have conditions in which it cannot provide its intended function, the recommended approach is to display an alert for the user that indicates the nature of the problem and possible actions the user could take - turning on WiFi, enabling Location Services, etc. Allow the user to terminate the application at their own discretion.

WARNING: It is possible to quit the application by calling exit. Applications calling exit will appear to the user to have crashed, rather than performing a graceful termination and animating back to the Home screen. Such usage provides a negative experience and is strongly discouraged.



Canardian(Posted 2011) [#34]
The warning part sounds good, that's how I implemented it on iOS too :)
So now I have implemented all targets except XNA. I don't have an eggsbox, so I can only implement it on a theoretical level, but maybe someone can test it when it's ready?


AdamRedwoods(Posted 2011) [#35]
Good info. Could this be put into the Monkey documentation somewhere (articles maybe)?

It took me a while to search for it.


Paul - Taiphoz(Posted 2012) [#36]
yeah I just this after looking for a while, thankfully my app is just being worked on in html5 and flash but its final target will be WP7 and I really wana stop it dead when the player hits exit.

glad I found this. should really be some where more prominent.


smilertoo(Posted 2012) [#37]
was a way to exit an app completely ever supplied? WP7 doesnt seem to have a kill app method the way ios does to remove background apps which means it keeps going in the background and messes up the back key for other things.

Better forumn search suggests error"" will do this, i'll test tomorrow.


therevills(Posted 2012) [#38]
The offical way to kill the app is Error "", although if you are using Diddy you can just call ExitApp which just wraps Error "".

And if you change screens to game.exitScreen it will call ExitApp for you.


Samah(Posted 2012) [#39]
@therevills: And if you change screens to game.exitScreen it will call ExitApp for you.

And if you FadeToScreen(Null) it will do that too. :)