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 betweenActivities
. - 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 theService
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'sonDestroy()
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:
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
Post a Comment for "How Can I Do Some Cleanup Right Before An Android App Is Quitting?"