Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can Dagger be used to perform injection on a Content Provider?

Tags:

android

dagger

I have recently been integrating Dagger into a project that uses ContentProviders. I create a single ObjectGraph instance in my custom Application object, and basically in each managed component:

  • Activity,
  • Fragment,
  • Service

... Then, I call getApplication(), downcast to my custom Application object, and force the injection through some custom implementation in my Application class. This seems to be the prescribed method of performing injection based on the samples I've seen posted by the guys at Square.

This pattern doesn't hold for ContentProvider instances though as their lifecycle isn't as predictably tied to the lifecycle of the Application object, ie ContentProviders can be, and as I'm observing frequently are, created before the Application object is created (for reasons I have yet to comprehend).

so... does anyone have a nice way of injecting ContentProviders using Dagger? I've so far made it by having an isInjected() call at the beginning of each of my ContentProvider's interface methods (insert, query, update, delete)... basically a hacky form of lazy initialization. But this seems far from ideal. Is there a more prescribed approach to injecting ContentProviders?

like image 622
homerman Avatar asked May 26 '14 14:05

homerman


People also ask

Can a Dagger inject a private field?

Dagger cannot support private fields and still support code-generated adapters (to avoid reflection). The way systems like Guice support private fields is they change the access to the field reflectively before accessing them.

What is the use of Dagger?

Dagger automatically generates code that mimics the code you would otherwise have hand-written. Because the code is generated at compile time, it's traceable and more performant than other reflection-based solutions such as Guice. Note: Use Hilt for dependency injection on Android.

What is Dagger injection?

Dagger is a fully static, compile-time dependency injection framework for Java, Kotlin, and Android. It is an adaptation of an earlier version created by Square and now maintained by Google.

What is difference between Hilt and Dagger?

In Dagger, we create scope annotations such as ActivityScope, FragmentScope to specify the lifecycle, but hilt provides us with core components such as Application, Activity, Fragment, Service, and View.


1 Answers

The Application subclass is just a convention since it's usually the first object created. Our apps do not have content providers which is why we use them. There's nothing that says you can't put it somewhere else.

You can just use the traditional singleton pattern for instantiating and holding a reference to the ObjectGraph.

public final class Dagger {
  static ObjectGraph og;

  static ObjectGraph og() {
    if (og == null) {
      og = ObjectGraph.create(..);
    }
    return og;
  }
}

The first person to access will initialize the instance which will be used for the lifetime of the process.

If your content provider is in a different process than your main application this solution will still work. Or you could simply create the graph when your content provider is created since it will be the only consumer. Normal multi-process rules still apply, of course, so no instances will be shared with the other processes.

like image 71
Jake Wharton Avatar answered Oct 20 '22 07:10

Jake Wharton