13

From the documentation for android.app.Application:

Base class for those who need to maintain global application state.

I am using my own subclass to maintain an object that I'm using to query a server. Also from the documentation:

onTerminate() Called when the application is stopping.

However, onTerminate() in my class is never called. I press the back button while viewing my main activity, and everything seems to shut down. My main activity's onDestroy() method is called and isFinishing() returns true, but my android.app.Application's onTerminate() method is never called.

Why is this? What am I missing? Is there something that is keeping it open?

Quality Catalyst
  • 6,531
  • 8
  • 38
  • 62
synic
  • 26,359
  • 20
  • 111
  • 149
  • Try using gallery, browser, launch a heavyweight game so that the platform would lack resources. Still not called? – yanchenko Mar 19 '10 at 23:29
  • Try installing a signal handler for SIGKILL from NDK. Not sure if this will work, and you won't be able to do much in it anyway... – Seva Alekseyev Aug 28 '12 at 16:01

2 Answers2

20

This might be related to the fact that although you exit the application, the process for it is still running in the background so that restarting the application would use the same process.

Also the javadocs states not to rely on the onTerminate code to be called.

Note: never depend on this method being called; in many cases an unneeded application process will simply be killed by the kernel without executing any application code.

Moritz
  • 10,124
  • 7
  • 51
  • 61
  • 13
    Note that the docs have been updated to be more explicit: this fn will never get called on a production Android device. – Tom Mar 05 '12 at 18:38
  • Which is against C++ spec as well, because kill means destructor(s) never get called (same as crash), which for example causes corrupted save-data. – Top-Master Nov 20 '22 at 16:00
  • At least iOS does follow C++ spec (as far as calling destructors is concerned). – Top-Master Nov 20 '22 at 16:00
5
void onTerminate()

This method is for use in emulated process environments.

http://developer.android.com/reference/android/app/Application.html

AZ_
  • 21,688
  • 25
  • 143
  • 191