Reputation: 6858
I'm curious if someone can explain the lifecycle of a ContentProvider. I'm especially interested in if, when and under what circumstances a provider is destroyed. That doesn't appear to be covered by the documentation.
The ContentProvider section of this article talks about documentation being hard to come by but it includes a link to a Google Groups discussion where an engineer at Google left a quick response, stating that "content providers are never destroyed. They exist for the entire lifetime of their process."
Okay, so maybe a ContentProvider lives as long as its process but how long does its process live? Suppose I have an application that provides a ContentProvider and a query from another application is the only reason my ContentProvider's process was created (i.e., there's not also an Activity or Service running.) Would that process really continue to run indefinitely? When Android is running low on resources, it destroys components like Services. Are ContentProviders not also candidates for being destroyed when resources are tight?
Upvotes: 15
Views: 8655
Reputation: 2792
Content Providers don't have a particular lifecycle, there is not much you can do to control it. The framework manages the creation and destruction of a Content Provider(process).
When external clients or components outside of the process make a request to the Content Provider, that request is processed in a thread from a thread pool of the process the provider resides in. And when there are no more requests the process can be reclaimed if there are no more active components (activity, service) in that process. But that is a decision Android takes.
Upvotes: 0
Reputation: 1007349
I'm especially interested in if, when and under what circumstances a provider is destroyed
It is created when your process is started (even before your Application
object is created), and it lives until the process is terminated.
how long does its process live?
That varies based upon what is going on with the app, the user, and the device.
Would that process continue to run indefinitely?
No.
When the system is running low on resources, Android destroys components like Services.
No, it does not. When the system is running low on RAM, Android terminates processes.
Are ContentProviders not also candidates for being destroyed when resources are tight?
A process containing a ContentProvider
can be terminated, whether due to low memory conditions, old age, user action, etc.
Upvotes: 16