On Wed, Feb 23, 2011 at 9:15 AM, Jake Colman <[email protected]> wrote:
> Is it possible for a service's
> onCreate() method to be called after it was already created (which would
> screw up my booleans) even though the service is already running?

No.

> Actually, maybe a better question is whether Android might be calling
> onDestroy() even though I haven't asked it to!

Absolutely. Also, onDestroy() may not be called.

Services cannot and will not live forever. Android will shut down
background services (i.e., ones that have not called
startForeground()) after a while. Depending on your onStartCommand()
return value, the service may restart, in which case onCreate() would
be called again.

And, if Android is in a hurry, it may not wait for onDestroy(), but
rather terminate the process entirely. I believe this also occurs if
the user closes your service via the Settings screen. And, on Android
2.1 and earlier, task killers also terminate your service without
calling onDestroy().

> If that is the case,
> what would cause that to happen given that my appwidget is regularly
> using the service?

A service in support of an app widget, IMHO, should not be trying to
live forever. It should be an IntentService, to do a little bit of
work, then get the heck out of RAM.

> And if this is something I have to deal with, how do
> I preserve state across this situation?

Files. Databases.

> And how do I distinguish
> between the service being destroyed because Android chose to do it (in
> which case I need to preserve my state) and it being destroyed because
> the widget was deleted from the homescreen (in which case I do not need
> to preserve my state)?

Implement the appropriate methods in AppWidgetProvider to determine
when the app widget is deleted. You will need the corresponding
actions in your <intent-filter> for the AppWidgetProvider as well,
IIRC.

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com | http://github.com/commonsguy
http://commonsware.com/blog | http://twitter.com/commonsguy

Android App Developer Books: http://commonsware.com/books

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to