1

A few times in Android's components I've encountered a sentence

... can be instantiated by the system

in particular here

Whether or not the content provider can be instantiated by the system — "true" if it can be, and "false" if not

So what does it mean with regard to Content Provider? (BTW Content Provider component is new to me - I know other Android components)

  1. When it can happen Content Provider is instantiated by system?
  2. Why it can happen?
  3. What benefit my app gets from this by-system instantiation?
Marian Paździoch
  • 8,813
  • 10
  • 58
  • 103
  • It means you don't create instances of app components with `new`. The system handles it – Mike M. Apr 19 '16 at 07:10
  • Do you ever instances CP with new? I don't think so... – Marian Paździoch Apr 19 '16 at 07:13
  • Nope. Just like `Activities` and `Service`s, you can't use `new` to instantiate a `ContentProvider`, and have it work correctly. – Mike M. Apr 19 '16 at 07:14
  • IMO a benefit of system-instantiated is: the CP could be located in other app, and marked as public (`android:exported="true"`) so if you want to use `new` you would have to include classes of this CP in your project which could be hard or not possible. while "system" doing this for you add flexibility and easiness – Yazan Apr 19 '16 at 07:20
  • Check some ideas http://stackoverflow.com/a/8272493/5766983 So you can control (enable/disable) free vs payed features of your app. – Maxim G Apr 19 '16 at 07:40

2 Answers2

0

I could not answer the 3 questions separately, as they intersect somehow, so here is a little answer, i hope it's correct, and i hope you find it useful:

as of Why/when

You should not use new you have to use getContentResolver() which creates instance and then you can call proper method: query(), update(), ...

getContentResolver().update(...);

As of benefits

If the CP is located in other app, and marked as public

android:exported="true"

then you can use it from your app, but how ? and by using i mean query/delete/update...

If you want to use new you would have to include classes/jar/resources... of this CP in your project which could be hard or not possible. while "system" doing this for you getContentResolver() provides (for publisher and consumer):

  1. flexibility
  2. easiness
  3. more secure
Yazan
  • 6,074
  • 1
  • 19
  • 33
0

All the android application components Activity, Service, ContentProvider and BroadcastReceiver(except dynamic) are instantiated by the system when ever a request comes.

Ex:

Activity is instantiated by the system when ever someone calls startActivity() with an Intent

Similarly, ContentProvider is also instantiated by the system when somebody request to access the data provided by the ContentProvider using ContentResolver.query or ContentResolver.insert by passing the respective URI.

sujith
  • 2,421
  • 2
  • 17
  • 26