I’m writing a SIP application using an SDK. My application has many Activities and so I initialise and shutdown the SDK using a service’s onCreate and onDestroy methods so that I can run it for the duration of my Application (not just an individual Activity).
The example application I’m working from calls startService and then just leaks the service – I don’t want to do this but I’m not sure that I have an alternative.
What I would like to do is bind to the service using Context.BIND_AUTO_CREATE in my Activity base class’s OnCreate method and unbind in OnDestroy. All of my Activities extend from this, so I would then have the Service available in all of my Activities.
But, what happens when the configuration changes or I switch activities? Does the service get aggressively killed in the brief period between activities, or is there a guarantee that it won’t? If it’s the former, doesn’t that slightly cripple the use of services? What design pattern should I be using to make something persist for exactly the lifetime of my Application?
I’ve realised I’m asking the wrong question. I’m assuming the Application will be destroyed when the user finishes with it, which it probably won’t, it’ll just sit in the background, and so will my service. I think I need a redesign.
Furthermore if I really wanted to continue down that path (which I now don’t), I could bind the service to my application, it would then exist for the lifetime of the application (which, as I remembered, will be indefinite unless the user kills it or Android reclaims it). In this instance I don’t need to specifically call unbind, as the binding will be destroyed along with the application.