SlideShare uma empresa Scribd logo
1 de 11
Baixar para ler offline
Dagger
Presenter : Ramesh Akula
http://about.me/aramesh
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.squareup.dagger:dagger:1.2.+'
provided 'com.squareup.dagger:dagger-compiler:1.2.+'
}
Gradle Dependencies
❖ @Module : Identify this class as a Dagger module.
❖ @Inject : Classes where this module is going to inject any of
its dependencies. Specify those classes that are directly
injected into object graph.
❖ @Provides: Identify method as an injection provider. The name
of the method doesn’t matter, it only relies on what class type
is provided.
Introduction
@Module(
complete = false,
library = true
)
public class DemoModule {
//Provide log object
@Provides
@Singleton
public Log provideLog() {
return new LogImpl();
}
}
Create a Module (@Module)
@Module(
injects = {
BaseApp.class,
MainActivity.class,
},
includes = {
DemoModule.class,
}
)
public class AppModule {
private BaseApp app;
public AppModule(BaseApp app) {
this.app = app;
}
@Provides
@Singleton
public Context provideApplicationContext() {
return app;
}
}
Create a Root Module (@Module)
public class BaseApp extends Application {
private ObjectGraph objectGraph;
@Override
public void onCreate() {
super.onCreate();
injectDependencies();
}
private void injectDependencies() {
objectGraph = ObjectGraph.create(new AppModule(this));
objectGraph.inject(this);
}
public void inject(Object object) {
objectGraph.inject(object);
}
}
Create an Object graph
public class MainActivity extends AppCompatActivity {
@Inject
Log mLog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Add to the object graph.
((BaseApp) getApplication()).inject(this);
}
Inject Dependencies (@Inject)
@Module(
complete = false,
library = true
)
public class DemoModule {
//……
@Provides
@Singleton
@Named(“login”)
public PreferencesEndPoint provideLoginPreferencesEndPoint(final Context context)
{
return new PreferenceEndPointImpl(context, “login”);
}
@Named - annotation
public class MainActivity extends AppCompatActivity {
@Inject
Log mLog;
@Inject
@Named(“login”)
PreferencesEndPoint loginEndPoint;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Add to the object graph.
((BaseApp) getApplication()).inject(this);
//……
}
@Named - annotation
public class MainActivity extends AppCompatActivity {
@Inject
Log mLog;
@Inject
Provider<Log> mLogProvider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Add to the object graph.
((BaseApp) getApplication()).inject(this);
//……
// Request new object every time requested
Log log = mLogProvider.get();
}
@Inject Provider
Thank You!

Mais conteúdo relacionado

Mais procurados (19)

mediator
mediatormediator
mediator
 
python and database
python and databasepython and database
python and database
 
C++
C++C++
C++
 
Cloudproject
CloudprojectCloudproject
Cloudproject
 
Constructor overloading & method overloading
Constructor overloading & method overloadingConstructor overloading & method overloading
Constructor overloading & method overloading
 
Classes & objects new
Classes & objects newClasses & objects new
Classes & objects new
 
Database programming
Database programmingDatabase programming
Database programming
 
React hooks
React hooksReact hooks
React hooks
 
Active Object
Active ObjectActive Object
Active Object
 
ConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTIONConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTION
 
Constructor&amp; destructor
Constructor&amp; destructorConstructor&amp; destructor
Constructor&amp; destructor
 
New Microsoft Word Document.doc
New Microsoft Word Document.docNew Microsoft Word Document.doc
New Microsoft Word Document.doc
 
Servlet session 9
Servlet   session 9Servlet   session 9
Servlet session 9
 
Java DataBase Connectivity - JDBC Part-2
Java DataBase Connectivity - JDBC Part-2Java DataBase Connectivity - JDBC Part-2
Java DataBase Connectivity - JDBC Part-2
 
Ian 2014.10.24 weekly report
Ian 2014.10.24 weekly reportIan 2014.10.24 weekly report
Ian 2014.10.24 weekly report
 
Dynamic databinding
Dynamic databindingDynamic databinding
Dynamic databinding
 
Python my SQL - create table
Python my SQL - create tablePython my SQL - create table
Python my SQL - create table
 
Constructor destructor.ppt
Constructor destructor.pptConstructor destructor.ppt
Constructor destructor.ppt
 
JVM and OOPS Introduction
JVM and OOPS IntroductionJVM and OOPS Introduction
JVM and OOPS Introduction
 

Destaque

Destaque (18)

Raising the efficiency of the open education system
Raising the efficiency of the open education system Raising the efficiency of the open education system
Raising the efficiency of the open education system
 
Panorámicas
PanorámicasPanorámicas
Panorámicas
 
A previdência do servidor público estadual - Ari Lovera
A previdência do servidor público estadual - Ari LoveraA previdência do servidor público estadual - Ari Lovera
A previdência do servidor público estadual - Ari Lovera
 
Fotos
FotosFotos
Fotos
 
Dedinho
DedinhoDedinho
Dedinho
 
Plano de 5ªaula 5ªserie 6º ano gestar 2012 cópia
Plano de 5ªaula 5ªserie 6º ano gestar 2012   cópiaPlano de 5ªaula 5ªserie 6º ano gestar 2012   cópia
Plano de 5ªaula 5ªserie 6º ano gestar 2012 cópia
 
Ester - Bachelor's degree Diploma
Ester - Bachelor's degree DiplomaEster - Bachelor's degree Diploma
Ester - Bachelor's degree Diploma
 
No Estés Triste
No Estés TristeNo Estés Triste
No Estés Triste
 
British and American
British and AmericanBritish and American
British and American
 
Jovens Brasileiros e o Mundo Mobile
Jovens Brasileiros e o Mundo MobileJovens Brasileiros e o Mundo Mobile
Jovens Brasileiros e o Mundo Mobile
 
American Express
American ExpressAmerican Express
American Express
 
Proyecto bloque 2
Proyecto bloque 2Proyecto bloque 2
Proyecto bloque 2
 
027. Superhik Alkohol Prijeti
027. Superhik   Alkohol Prijeti027. Superhik   Alkohol Prijeti
027. Superhik Alkohol Prijeti
 
PEPTIC ULCER
PEPTIC ULCERPEPTIC ULCER
PEPTIC ULCER
 
The Voicemail Script Part 4
The Voicemail Script Part 4The Voicemail Script Part 4
The Voicemail Script Part 4
 
Audience Feedback
Audience FeedbackAudience Feedback
Audience Feedback
 
Working Title Part 6
Working Title Part 6Working Title Part 6
Working Title Part 6
 
Tablas
Tablas Tablas
Tablas
 

Semelhante a Dagger1

Idiomatic gradle plugin writing
Idiomatic gradle plugin writingIdiomatic gradle plugin writing
Idiomatic gradle plugin writingSchalk Cronjé
 
Idiomatic Gradle Plugin Writing
Idiomatic Gradle Plugin WritingIdiomatic Gradle Plugin Writing
Idiomatic Gradle Plugin WritingSchalk Cronjé
 
Idiomatic Gradle Plugin Writing
Idiomatic Gradle Plugin WritingIdiomatic Gradle Plugin Writing
Idiomatic Gradle Plugin WritingSchalk Cronjé
 
Idiomatic Gradle Plugin Writing
Idiomatic Gradle Plugin WritingIdiomatic Gradle Plugin Writing
Idiomatic Gradle Plugin WritingSchalk Cronjé
 
Gradle - small introduction
Gradle - small introductionGradle - small introduction
Gradle - small introductionIgor Popov
 
Global objects in Node.pdf
Global objects in Node.pdfGlobal objects in Node.pdf
Global objects in Node.pdfSudhanshiBakre1
 
Chapter 13 introduction to classes
Chapter 13 introduction to classesChapter 13 introduction to classes
Chapter 13 introduction to classesrsnyder3601
 
Sharper Better Faster Dagger ‡ - Droidcon SF
Sharper Better Faster Dagger ‡ - Droidcon SFSharper Better Faster Dagger ‡ - Droidcon SF
Sharper Better Faster Dagger ‡ - Droidcon SFPierre-Yves Ricau
 
Bca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objectsBca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objectsRai University
 
Mca 2nd sem u-2 classes & objects
Mca 2nd  sem u-2 classes & objectsMca 2nd  sem u-2 classes & objects
Mca 2nd sem u-2 classes & objectsRai University
 
Dependency injection using dagger2
Dependency injection using dagger2Dependency injection using dagger2
Dependency injection using dagger2Javad Hashemi
 
Basic Gradle Plugin Writing
Basic Gradle Plugin WritingBasic Gradle Plugin Writing
Basic Gradle Plugin WritingSchalk Cronjé
 
Chapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part IIChapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part IIEduardo Bergavera
 
Java căn bản - Chapter7
Java căn bản - Chapter7Java căn bản - Chapter7
Java căn bản - Chapter7Vince Vo
 
object oriented programming using java, second sem BCA,UoM
object oriented programming using java, second sem BCA,UoMobject oriented programming using java, second sem BCA,UoM
object oriented programming using java, second sem BCA,UoMambikavenkatesh2
 

Semelhante a Dagger1 (20)

Idiomatic gradle plugin writing
Idiomatic gradle plugin writingIdiomatic gradle plugin writing
Idiomatic gradle plugin writing
 
Idiomatic Gradle Plugin Writing
Idiomatic Gradle Plugin WritingIdiomatic Gradle Plugin Writing
Idiomatic Gradle Plugin Writing
 
Idiomatic Gradle Plugin Writing
Idiomatic Gradle Plugin WritingIdiomatic Gradle Plugin Writing
Idiomatic Gradle Plugin Writing
 
Di code steps
Di code stepsDi code steps
Di code steps
 
Idiomatic Gradle Plugin Writing
Idiomatic Gradle Plugin WritingIdiomatic Gradle Plugin Writing
Idiomatic Gradle Plugin Writing
 
Class and object in C++ By Pawan Thakur
Class and object in C++ By Pawan ThakurClass and object in C++ By Pawan Thakur
Class and object in C++ By Pawan Thakur
 
Java Programming - 04 object oriented in java
Java Programming - 04 object oriented in javaJava Programming - 04 object oriented in java
Java Programming - 04 object oriented in java
 
Gradle - small introduction
Gradle - small introductionGradle - small introduction
Gradle - small introduction
 
Global objects in Node.pdf
Global objects in Node.pdfGlobal objects in Node.pdf
Global objects in Node.pdf
 
Chapter 13 introduction to classes
Chapter 13 introduction to classesChapter 13 introduction to classes
Chapter 13 introduction to classes
 
Sharper Better Faster Dagger ‡ - Droidcon SF
Sharper Better Faster Dagger ‡ - Droidcon SFSharper Better Faster Dagger ‡ - Droidcon SF
Sharper Better Faster Dagger ‡ - Droidcon SF
 
Bca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objectsBca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objects
 
Mca 2nd sem u-2 classes & objects
Mca 2nd  sem u-2 classes & objectsMca 2nd  sem u-2 classes & objects
Mca 2nd sem u-2 classes & objects
 
Dependency injection using dagger2
Dependency injection using dagger2Dependency injection using dagger2
Dependency injection using dagger2
 
Basic Gradle Plugin Writing
Basic Gradle Plugin WritingBasic Gradle Plugin Writing
Basic Gradle Plugin Writing
 
OOPs & Inheritance Notes
OOPs & Inheritance NotesOOPs & Inheritance Notes
OOPs & Inheritance Notes
 
Java notes
Java notesJava notes
Java notes
 
Chapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part IIChapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part II
 
Java căn bản - Chapter7
Java căn bản - Chapter7Java căn bản - Chapter7
Java căn bản - Chapter7
 
object oriented programming using java, second sem BCA,UoM
object oriented programming using java, second sem BCA,UoMobject oriented programming using java, second sem BCA,UoM
object oriented programming using java, second sem BCA,UoM
 

Último

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 

Último (20)

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 

Dagger1

  • 1. Dagger Presenter : Ramesh Akula http://about.me/aramesh
  • 2. dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.squareup.dagger:dagger:1.2.+' provided 'com.squareup.dagger:dagger-compiler:1.2.+' } Gradle Dependencies
  • 3. ❖ @Module : Identify this class as a Dagger module. ❖ @Inject : Classes where this module is going to inject any of its dependencies. Specify those classes that are directly injected into object graph. ❖ @Provides: Identify method as an injection provider. The name of the method doesn’t matter, it only relies on what class type is provided. Introduction
  • 4. @Module( complete = false, library = true ) public class DemoModule { //Provide log object @Provides @Singleton public Log provideLog() { return new LogImpl(); } } Create a Module (@Module)
  • 5. @Module( injects = { BaseApp.class, MainActivity.class, }, includes = { DemoModule.class, } ) public class AppModule { private BaseApp app; public AppModule(BaseApp app) { this.app = app; } @Provides @Singleton public Context provideApplicationContext() { return app; } } Create a Root Module (@Module)
  • 6. public class BaseApp extends Application { private ObjectGraph objectGraph; @Override public void onCreate() { super.onCreate(); injectDependencies(); } private void injectDependencies() { objectGraph = ObjectGraph.create(new AppModule(this)); objectGraph.inject(this); } public void inject(Object object) { objectGraph.inject(object); } } Create an Object graph
  • 7. public class MainActivity extends AppCompatActivity { @Inject Log mLog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //Add to the object graph. ((BaseApp) getApplication()).inject(this); } Inject Dependencies (@Inject)
  • 8. @Module( complete = false, library = true ) public class DemoModule { //…… @Provides @Singleton @Named(“login”) public PreferencesEndPoint provideLoginPreferencesEndPoint(final Context context) { return new PreferenceEndPointImpl(context, “login”); } @Named - annotation
  • 9. public class MainActivity extends AppCompatActivity { @Inject Log mLog; @Inject @Named(“login”) PreferencesEndPoint loginEndPoint; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //Add to the object graph. ((BaseApp) getApplication()).inject(this); //…… } @Named - annotation
  • 10. public class MainActivity extends AppCompatActivity { @Inject Log mLog; @Inject Provider<Log> mLogProvider; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //Add to the object graph. ((BaseApp) getApplication()).inject(this); //…… // Request new object every time requested Log log = mLogProvider.get(); } @Inject Provider