ContentProvider destruction/lifecycle
Asked Answered
K

2

15

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?

Karame answered 4/6, 2014 at 20:45 Comment(0)
S
16

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.

Suspensor answered 4/6, 2014 at 20:49 Comment(4)
Any chance you can provide some supporting documentation for the first point? Just would like to read more.Tannate
@notJim: It's not documented -- I wish it were. And actually the story is a bit more complicated than I made it out to be, as I wound up doing some testing on this point sometime after I wrote this answer. Roughly speaking, your ContentProvider instances are created contemporaneously with your Application instance. I forget the precise sequence of their constructors and onCreate() calls.Suspensor
It is created when your process is started (even before your Application object is created), and it lives until the process is terminated...Does that mean that when my app is killed content provider is destroyed since process dies?Defer
@Stranger: Correct.Suspensor
L
0

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.

Lucretialucretius answered 4/6, 2014 at 21:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.