android


Dagger 2 injection of fields inside classes other than Activities


I have a class other than an Activity with injected fields :
public Class1 extends Class2 {
#Inject A a;
}
public Class2 extends Class3 {
#Inject B b;
}
public Class3 {
#Inject C c;
}
And I am providing Class1 like this:
#Provides
Class1 provideClass1(){
return new Class1();
}
My question : when providing Class1 will Dagger inject B and C at the level of the super class? Or should I use constructor injection and call super inside the constructors?
Activity, Fragment, and Service are ideal sites for property injection because you have no control over their constructor and their instantiation is handled by the Android OS.
For the rest of your classes, constructor injection gives much more control and makes it easier to unit test. When writing a unit test, you simply pass in the test doubles you want when you are initialising the system under test. You also make it easier to write immutable classes fulfilling Effective Java item 15.
As for your other question, Dagger 2 is smart enough to inject properties in a super class. In other words, if you have a ConcreteService extends BaseService and you call component.inject(this) inside ConcreteService Dagger 2 will know to inject the properties of BaseService if they are annotated correctly.
However, in your example you have a mix of property injection (the #Inject annotations inside Class1 and Class2) and constructor injector (the #Provides method for Class1 which simply uses the constructor). This is not enough to have all the properties in the super classes of Class1 to be injected because you have specified the medium of provision of Class1 to be a simple call to the constructor. On the other hand, if you obtained the instance of Class1 through a component (through provision methods) or through requesting injection on a class which has a #Inject annotated Class1 property then you would get a fully-formed instance.

Related Links

Slight latency in recycler view scrolling with multiple view holders
VPS as video relay server using ffmpeg and ffserver?
SntpClient and Android's Volley
Wrap AcroFields TextField in ItextG
Android app crashes if I declare more than 3 Drawables
SVG Path conversion to Android Drawable fails
That does not contain a constructor that takes 3 arguments at base(context,0,items) in BaseAdapter
test for internet connection in android login
Admob House Ad only when no fill
ImageButton background not changing
Triggering An Event At Certain Dates Android
Error:Conflict with dependency 'com.android.support: when update from 23.1.1 to 23.4.0
How to hide navigation drawer when opening certain fragment?
INSTALL_REFERRER intent gets fired continuously with some random UTM parameters
CSV file not uploading in android application from any file manager in motorola phone but works fine in lava
android - Error in passing string fragment to fragment

Categories

HOME
beautifulsoup
codenvy
c99
jmeter
xbee
tivoli
nsbundle
parameters
limit
gluon-mobile
retrofit2
barcode
teechart
project
jquery-ui-sortable
dkim
gson
java.util.logging
onedrive-api
flash-player
dimensional-modeling
foreign-keys
connector
gmm
blade
ksh
gulp-watch
ember-cli
roslyn
itunes
custom-post-type
spotipy
corenlp-server
mobx
postback
weather
cart
git-squash
github3.py
magento-1.9.2.4
projects-and-solutions
css-position
escpos
softmax
oracle-service-bus
smoothstate.js
procfile
jquery-ui-draggable
robomongo
amq
static-cast
azure-cli
context-sensitive-grammar
objectscript
gets
omxplayer
hadoop-2.7.2
redbeard-ios
seek
microstation
myspace
watch-os-2
neon
simevents
jtwig
http-status-code-401
mediametadataretriever
deep
nss
cda
nullable
aspen
qbxml
relativelayout
sapi
forwarding
ember-cli-rails
zscript
mirror
node-serialport
code-documentation
dbaccess
beatsmusic
viewer
sonarqube-5.0
wcm
insight.database
menubar
mysql-error-1044
route-provider
diem
alice
asplinkbutton
extreme-programming
subgraph
kqueue
android-holo-everywhere
jqote
drawtobitmap
xui
interprocess
opml
mathematical-notation

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App