Content Provider's onCreate getting called before I tried accessing it

81 Views Asked by At

According to the Official documentation

onCreate() method of the content provider is called by Android's system call immediately after it creates the provider. And since the provider isn't created until a ContentResolver object tries to access it. How my onCreate is getting called before I tried accessing it using contentResolver? (Note: I am not even accessing the provider still its onCreate getting called)

2

There are 2 best solutions below

6
David Wasser On BEST ANSWER

The documentation for Provider.onCreate() that you linked to explicitly states:

Implement this to initialize your content provider on startup. This method is called for all registered content providers on the application main thread at application launch time.

So Android will call onCreate() on all content providers immediately on application launch. Obviously it cannot call onCreate() until the content provider is instantiated, so that means that immediately on application launch, Android will instantiate all content providers and call onCreate() on all of them.

Your application will have to deal with this appropriately.

0
blackapps On

One never has to start/launch the app that comes with a content provider.

The provider will do its job without the app ever started.

As soon as the app is installed.

So expect your onCreate() to be called at install too.