View Full Version : Strange exit behavior on android

05-27-2011, 05:07 PM

I've encountered some strange behavior on android when trying to exit when I have a service running.
Specifically: When the activity launches a service, exiting with the back button will close that service. The service will pop up again after a few seconds (android restarts sticky services automatically), this should not happen: the service is not binded to the activity and should not close. Still, after the service restarts everything works right from there.
However, if I exit the activity with the home button, the service does not close (which is good). But if I then launch the activity from the service notification, it starts up a second instance of the activity (I assume; if I press back, the activity closes with sliding animation into the same activity). This should not happen, the flag in the notification is set to use the existing activity.

So to recap:
1. closing the activity closes the service, this is wrong but sidesteps 2.
2. if the activity is not closed but minimized (and the service remains alive), opening the activity starts a new activity instead of bringing the old one to front.

It seems to me the error is somewhere in proton, but I don't really have any idea what could cause this.

I hope you can help. Thanks!

05-29-2011, 02:19 AM
Hmm. This could be related to killing the app on exit in an incorrect way that I do..

Could try removing " android.os.Process.killProcess(android.os.Process. myPid()); " from the Java side.

Phil Hassey sent me stuff on how I should properly do the shutdown (so I don't need that PID thing), I need to wait before adding it until I can properly test it.

If you feel adventurous, here is his info:

Here's one thing I've found that you might want to change (maybe?):

Then you get a callback from your own code that says "quit the game"
you might want to do this instead of just killing your app right

// this pushes an event onto the UI/Event Thread from the GL thread
app.runOnUiThread(new Runnable(){
public void run() {

Then at the end of Activity.onDestroy():

android.os.Process.killProcess(android.os.Process. myPid()); //
HACK, this is totally nasty, but it ensures that all our resources are

.. I don't know that the PID kill there is even necessary, but it'll
help ensure that all game resources get eaten up and aren't lingering
at that point.

That change gives the app it's natural shutdown process instead of you
manually suspending your native code then just killing it abruptly.
Probably all same-difference, but seems a bit cleaner this way.


05-30-2011, 01:14 PM
Well it certainly has an effect.

If I include killing the process, then the double exit is fixed but there is still a noticeable delay (up to 2 seconds, something is still going on here), and the service disappearing problem is the same as it was before.

Without killing the process, the service is not destroyed when quitting. However when activating the app through the service, I get a blank screen for a second and then it and the service are destroyed anyway. The service will then reappear and everything behaves as before.

It's annoying because the documentation states the service should be independent and only stop when I call stopService() on it. Despite this it is clearly still related to the process.

05-30-2011, 11:30 PM
Interesting. So far I haven't done anything with services so I don't have any additional ideas - as far as I know I'm not doing anything else weird with the Android side of things.

If you notice anything wrong (the java stuff is in /shared/android ) please send me the fix/svn patch and I'll correct it.