0

I am looking for a place to put my clean up code (e.g. releasing file handles, shutting down services).

Here is the scenario: I have an app with multiple activities and a service running in the background. The service accesses a lot of sensors and files. Thus, it must be stopped if the app is terminated by the user to prevent battery draining. However, the service must continue running while the app is in the background and/or the screen is turned off. I am looking for a place to stop the service in code. onDestroy() of my Activities seems not to be a good idea because Android may call it (even while my app is running) or may call it never at all.

Is there a good place for releasing used resources of an app when the user knowingly terminates it?

  • possible duplicate of [Checking if an Android application is running in the background](http://stackoverflow.com/questions/3667022/checking-if-an-android-application-is-running-in-the-background) – Marius Aug 01 '14 at 08:01
  • No, detecting if the app is in foreground/background is not the problem. I need something like onDestroy() but not as volatile that will be called if the app exits. Something like an app deconstructor. – user3522584 Aug 01 '14 at 08:11
  • There is no callback for application process termination. The system will simply kill the least recently used process when it determines that it's running low on memory. Overall, you should release resources used by an `Activity` when it is stopped or destroyed, and reacquire them when it's restarted/recreated. – corsair992 Aug 01 '14 at 12:47

0 Answers0