Inject database in a ContentProvider with dagger

前端 未结 5 2433
无人及你
无人及你 2021-02-19 22:57

I want to inject a singleton SqliteOpenHelper in a ContentProvider. However, it seems that the ContentProvider instance is being built bef

5条回答
  •  余生分开走
    2021-02-19 23:15

    I faced the same issue and had to defer injection until the database was needed. You might be able to use Dagger's lazy injection to achieve the same effect.

    From the content provider's onCreate documentation:

    You should defer nontrivial initialization (such as opening, upgrading, and scanning databases) until the content provider is used

    Apparently this suggestion cannot be disregarded. Implementing the onCreate() method provides an example using an SQLiteOpenHelper with a content provider.

提交回复
热议问题