Skip to content Skip to sidebar Skip to footer

How Can I Do Some Cleanup Right Before An Android App Is Quitting?

Is there some sort of onTerminate() method where I can do some cleanup (I want to clear some SharedPreferences) when my Android app is terminating? I have an Activity that is keepi

Solution 1:

I've not tried this, but here's what I would do:

  • As Alex mentioned in the comment to original question, use a Service to share the app-wide state between Activities.
  • Whenever you move between Activities, bind to the service from the "new" activity, and unbind from the "old" one. Check this to understand how to coordinate activities.
  • If you follow this properly, you can ensure that at least one Activity is always bound to the Service as long as your app is running; and that all Activities are unbound when the app is no longer running - at which point your service's onDestroy() is called. This is where you perform your cleanup.

Solution 2:

So android doesn't really have a concept of an app being "finished". Unfortunently there is nothing synonymous to "onTerminate()". Is there some criteria by which you can decide when to clear your running average?

Solution 3:

Post a Comment for "How Can I Do Some Cleanup Right Before An Android App Is Quitting?"