3

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 keeping a running average of a few numbers, that I'm storing in a SharedPreference. I want this average to last while the app is running (the user can move between different activities) so I can't clear it on onDestroy() for that particular activity. I need to be able to clear it once the app is about to quit.

How can I do this?

2
  • Considering that there's no guarantee that you get any event other than onPause() before the app is closed.. does android support Runtime.addShutdownHook and does it work as you'd expect?
    – Voo
    Oct 28, 2011 at 2:25
  • Do you want your running average to not be clear as long as at least one activity is alive? If you hit the home button, should the average be cleared immediately or only when the os finally kills your activities? If the user just re starts your main activity can you clear the preferences then? If you only want these averages to last while your app is running, have you thought about a service instead?
    – Alex
    Oct 28, 2011 at 3:00

3 Answers 3

2

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.
0

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?

2
  • I guess if the app goes to the Home screen I can consider it "terminated", though there are edge cases that can deem this invalid, I am willing to make exceptions... Oct 28, 2011 at 2:29
  • Actually there does exist an onTerminate method in android.app.Application class .. however it is useless in this scenario since it is only called in emulated process environments and never on actual devices. Oct 28, 2011 at 3:25
0

Use SharedPreference.Editor to remove the preferences, and commit. Here's a link for you: http://developer.android.com/reference/android/content/SharedPreferences.Editor.html

2
  • That's not at all what he's asking about. Oct 28, 2011 at 2:21
  • I know how to remove the preference, I just need to figure out WHEN to remove it. Oct 28, 2011 at 2:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.