Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Actually, you have an error in your sample code. You have no injectable constructor - you cannot inject an instance method, only a constructor, and that method is never called. </p> <p>You should have:</p> <pre><code>public class XmlDataReader implements DataReader&lt;XmlPushable&gt; { protected final XmlPullParser mXmlPullParser; @Inject public XmlDataReader(XmlPullParser xmlPullParser) { mXmlPullParser = xmlPullParser; } } </code></pre> <p>This way it's a constructor, and will be properly injected. </p> <p>As to the overall approach, you're in the right direction. You <code>@Provide</code> <code>DataReader&lt;XmlPushable</code>, and effectively bind <code>XmlDataReader</code> to it in your provides method. So far so good. Dagger, when asked for <code>DataReader&lt;XmlPushable&gt;</code> will use that binding, and will effectively pass-through the dependency and provide <code>XmlDataReader</code> to satisfy this.</p> <p><code>XmlDataReader</code> has an <code>@Inject</code> annotated constructor, so Dagger's analysis will see it as an "injectable type" so you don't need an <code>@Provides</code> to provide it. As long as you either have an <code>XmlPullParser</code> that either has an <code>@Inject</code> annotated constructor or is provided in an <code>@Provides</code> method on a module, what you've coded here seems good, but is not quite complete.</p> <p>You don't have any entryPoints listed in your @Module, and you <strong><em>must</em></strong> have a class that will be the first thing you obtain from the ObjectGraph. So this code is reasonable as far as it goes, but incomplete. You need something that injects (through a field or constructor) <code>DataReader&lt;XmlPushable&gt;</code> For example, you might make a <code>FooActivity</code> which does this.</p> <p>Consider:</p> <pre><code>public class XmlDataReader implements DataReader&lt;XmlPushable&gt; { protected XmlPullParser mXmlPullParser; @Inject public XmlDataReader(XmlPullParser xmlPullParser) { mXmlPullParser = xmlPullParser; } } @Module(entryPoints = { FooActivity.class, BarActivity.class }) public class XmlServiceModule { @Provides DataReader&lt;XmlPushable&gt; provideDataReader(XmlDataReader dataReader) { return dataReader; } @Singleton @Provides XmlPullParserFactory parserFactory() { XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); factory.setNamespaceAware(true); return factory; } @Provides XmlPullParser parser(XmlPullParserFactory factory) { XmlPullParser xpp = factory.newPullParser(); } } public class YourApp extends Application { private ObjectGraph graph; @Override public void onCreate() { super.onCreate(); graph = ObjectGraph.get(new ExampleModule(this)); } public ObjectGraph graph() { return objectGraph; } } public abstract class BaseActivity extends Activity { @Override protected void onCreate(Bundle state) { super.onCreate(state); ((YourApp) getApplication()).objectGraph().inject(this); } } class FooActivity extends BaseActivity { @Inject DataReader&lt;XmlPushable&gt; reader; @Override public void onCreate(Bundle bundle) { super.onCreate(bundle); // custom Foo setup } } class BarActivity extends BaseActivity { @Inject DataReader&lt;XmlPushable&gt; reader; @Override public void onCreate(Bundle bundle) { super.onCreate(bundle); // custom Bar setup } } </code></pre> <p>This is probably close to what you want. All things are reachable from an entry point, and all things you depend on are bound. The two Activities are your "entry points" (roots in the object graph). When they are initialized, they ultimately call the <code>ObjectGraph</code>'s <code>inject(Object)</code> method on themselves, which causes Dagger to inject any <code>@Inject</code> annotated fields. Those fields are the <code>DataReader&lt;XmlPushable&gt;</code> fields, which is satisfy by XmlDataReader, which is provided with an XmlPullParser, which is provided by using a XmlPullParser. It all flows down the dependency chain.</p> <p>Small note - <code>@Module(complete=true)</code> is the default. You don't need to specify complete, unless it's incomplete and you specify <code>false</code></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload