Note that there are some explanatory texts on larger screens.

plurals
  1. POReinstall / Update application what happens to a running service?
    primarykey
    data
    text
    <p>I have database cleanup that I need to do within a running service thread. Under normal conditions, this will occur when the service is done and onDestroy() gets called.</p> <p>I've noticed that if I reinstall or update the application while the service is running that onDestroy() is not called for the service. Is there anyway my application can know at the next startup that it's the first run after a reinstall so it can complete some cleanup on the database?</p> <p>To clarify, simply running the service again won't do what I need. My service in many cases is started by a broadcastreceiver and not by the activity itself so I can't rely on the onPause for the main activity. I can't figure out how to know if the service might already be running or was silently killed and needs cleanup.</p> <p>I'd like to clarify since others seem to have misunderstood. First, onDestroy just plain doesn't happen in the situation of a reinstall or update of the app. Neither do the shared preferences get reset because the app was not uninstalled. Second, Google specifically states you can't rely on the onDestroy, so berating me for finding a quick fix instead of asking Google why onDestroy isn't working doesn't quite make sense either. See the API docs about <a href="http://developer.android.com/reference/android/app/Activity.html#onDestroy%28%29" rel="nofollow">onDestroy</a> for Activities. The service documentation doesn't repeat that warning but obviously it holds true. The only working solution I've found to date is the one I posted below from another StackOverflow thread.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COI find it very confusing if your service is not killed properly, and onDestroy() isn't called, once you reinstall your application. Maybe it was best to ask Google for some insight on the topic instead of finding a quick fix around the problem?
      singulars
    2. CO@Harism, Google does not guarantee that an onDestroy gets called ever. In fact, in the API docs they state "There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it, so it should not be used to do things that are intended to remain around after the process goes away." Specifically though if you reinstall / update an app whatever is running gets killed and onDestroy does not get called. The only event that is guaranteed to be called is onPause according to the documentation, and services don't have that event.
      singulars
    3. COTrue that. But personally I find it rather vague excuse not to call Service.onDestroy() similarly as Activity.onPause() gets called just because there's no Service.onPause() method. Quite the contrary actually, Service.onDestroy() clearly states to do proper cleanup before returning from it. It's a totally different story if Google doesn't care about possibly lost resources at that point and relies on killing the process only though.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload