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

code to rotate image captured by camera intent not working in Android
Eclipse empty all file contents from Android project, how to fix it?
How to Move image in SurfaceView from top to bottom only [closed]
Is there a way to find out the startActivity intent flags an activity was started with?
internal app can not import com.android.internal.policy.impl?
How to make call backs between Fragments of the same Ativity
Sony SmartWatch - what is the host application (AHA)
how to Get data from SQLite database to intent through “onItemClick” on item in ListView Android
Android Listview using Google maps
this code create folder in SD card but not save image in that folder
Google inApp onPurchaseStatechange
Run specify app which installed in phone
Async download of images to gridview from MediaStore
Glassfish server throwing exception when sending JSONObject through android
loss of information when passing a parcelable object to an intent
How can select random data from database and display in textview?

Categories

HOME
sonarqube
asp.net-web-api
search
facebook-oauth
apache-flink
object
youtube-data-api-v3
interface
solaris
r-googlesheets
ms-access-2016
xbee
x264
gluon-mobile
retrofit2
modal-dialog
rcloud
pattern-matching
drop-down-menu
why3
line
datastage
azure-iot-hub
dynamic-sql
foreign-keys
bourbon
oledb
asp.net-core-webapi
kendo-mobile
imageresizer
ms-access-web-app
android-maps
errbot
gp
nsurl
scatter3d
modalviewcontroller
google-knowledge-graph
sim-card
railstutorial.org
chef-solo
spring-mvc-test
resolution
wc
stylecop
uiimage
puredata
bluez
magic-draw
boxing
edit
testrail
xcode-server
async.js
watch-os-2
ng-grid
respondcms
nss
project-organization
false-sharing
contentcontrol
decimalformat
sciruby
iokit
dllimport
wiredep
ons-api
poppler
vacuum
nutiteq
snackbar
broadband
dbaccess
boost-program-options
iiop
angular-carousel
ignite-ui
mintty
square-bracket
getopt
isqlquery
dtmf
script#
tlf
cffile
workitem
tree-balancing
oracle-enterprise-linux
usertype
isa-swizzling
wiimote
flex-mobile
role
google-ajax-api
mbeans
event-receiver
http-daemon
unattended-processing
cac
luajava
silent
globals
non-clustered-index
ajax-polling
pagemethods
filesystemobject
wordprocessingml
windows-identity
java1.4
lemmatization

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