"Android Advanced Light"--dagger2

Source: Internet
Author: User

NO1:

Project's Build.gradle file add

buildscript{    dependencies{...        classpath ' com.neenbedankt.gradle.plugins:android-apt:1.8 '}    

Module:app's Build.gradle add

Apply plugin: ' com.neenbedankt.android-apt ' ... dependencies{...    Apt ' com.google.dagger:dagger-compiler:2.7 '    compile ' com.google.dagger:dagger:2.7 '}

No2:

@Inject, @Component

 Public class watch{    @Inject    public  Watch () {    }    publicvoid  Work () {    }}
@Component  Public Interface mainactivitycomponent{    void  inject (mainactivity activity);}

Call

 Public class extends appcompatactivity{    SuperonCreate (savedinstancestate);    Setcontentview (r.layout.activity_main);    Daggermainactivitycomponent.create (). Inject (this); // 2     watch.work ();}

No3:

@Module, @Provides

@InjectGson Gson;
@Module  Public class gsonmodule{    @Provides    public  Gson Providegson () {        returnNew  Gson ();    }}
@Component (modules = Gsonmodule.  Class)publicinterface  mainactivitycomponent{    void  Inject (mainactivity activity);}

Call

 Public class extends appcompatactivity{    @Inject    gson Gson;    @Override    protectedvoid  onCreate (Bundle savedinstancestate) {          Super. OnCreate (savedinstancestate);        Setcontentview (r.layout.activity_main);        Daggermainactivitycomponent.create (). Inject (this);         = "{' name ': ' Zhangwuji ', ' Age ':"} ";         = Gson.fromjson (Jsondata,man.class);}    }

No4:

@Named, @Qualifier

@Module  Public class enginemodule{    @Provides    @Named ("Gasoline")    public  Engine Providegasoline () {        returnnew  gasolineengine ();    }    @Provides    @Named ("Diesel")    public  Engine Providediesel () {         return New dieselengine ();    }}

Call

 Public class car{    private  engine engine;    @Inject    Public Car (@Named ("Diesel") engine engine) {        this. Engine =   engine;    }      Public String Run () {        return  engine.work ();    }}

-------------------------------------

  @Qualifier @retention (RUNTIME)  public  @interface   gasoline{}@ Qualifier@retention (RUNTIME)  public  @ Interface   diesel{}  
  @Module  public  class   enginemodule{@Provides @Gasonline  public Engine Providegasoline () { return  new   Gasolineengine (); @Provides @Diesel  public   Engine provide Diesel () { return  new   Dieselengine (); }}
 Public class car{    private  engine engine;    @Inject    public  Car (@Gasoline engine) {        this. Engine = engine;    }      Public String Run () {        return  engine.work ();    }}

NO5:

@Singleton, @Scope

@InjectGson Gson; @InjectGson gson1;
@Module  Public class gsonmodule{    @Singleton    @Provides    public  Gson Providegson () {          Returnnew  Gson ();}    }
= Gsonmodule. class )publicinterface  mainactivitycomponent{     void  Inject (Mainactiviy activity);}

Call

 Public class extends appcompatactivity{    privatestaticfinal String tag= "Dagger2";    @Inject    Gson Gson;    @Inject    Gson gson1;    @Override    protectedvoid  onCreate (Bundle savedinstancestate) {         Super . OnCreate (savedinstancestate);        Setcontentview (r.layout.activity_main);        Daggermainactivitycomponent.create (). Inject (this);}    }

-----------------------------

@Scope @documented@retention (RUNTIME)  public @interface singleton{}
@Scope @rentention (RUNTIME)  public @interface applicationscope{}
@Module  Public class gsonmodule{    @ApplicationScope    @Provides    public  Gson Providegson () {         returnnew  Gson ();}    }
@ApplicationScope @component (modules=gsonmodule. class )publicinterface  activitycomponent{    void  inject ( Mainactivity activity);     void inject (secondactivity activity);}
 Public classAppextendsapplication{activitycomponent activitycomponent; @Override Public voidonCreate () {Super. OnCreate (); Activitycomponent=Daggeractivitycomponent.builder (). build (); }     Public StaticAPP Get (Context context) {return(APP) context.getapplicationcontext (); } activitycomponent getactivitycomponent () {returnactivitycomponent; }}

Call

 Public classMainactivityextendsappcompatactivity{Private Static FinalString tag= "Dagger2"; PrivateButton Bt_jump;    @Inject Gson Gson;    @Inject Gson Gson1; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); App.get (mainactivity. This). Getactivitycomponent (). Inject ( This);    OnClick (); }    Private voidOnClick () {bt_jump=(Button) Findviewbyid (r.id.bt_jump); Bt_jump.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (view view) {Intent Intent=NewIntent (mainactivity. This, Secondactivity.class);            StartActivity (Intent);    }        }); }}

NO6:

@Component

 public  class   swordsman{@Inject  public   swordsman{}  public   String    Fighting () { return  "for the tree, MO and grass contend" ; }}
  @Module  public  class   swordsmanmodule{@Provides  public   Swordsman Provideswordsman () { return  new   Swordsman (); }} @Component (Modules  = Swordsmanmodule. class  )  public   Interface   swordsmancomponent{swordsman Getswordsman ();}  
= Gsonmodule. class, dependencies = swordsmancomponent. class )publicinterface  activitycomponent{    void  inject ( Mainactivity activity);     void inject (secondactivity activity);}
 Public classAppextendsapplication{activitycomponent activitycomponent; @Override Public voidonCreate () {Super. OnCreate (); Activitycomponent=Daggeractivitycomponent.builder ();    Swordsmancomponent (Daggerswordsmancomponent.builder (). Build ()). build (); }     Public StaticAPP Get (Context context) {return(APP) context.getapplicationcontext (); } activitycomponent getactivitycomponent () {returnactivitycomponent; }}

Call

 Public class extends appcompatactivity{    @Inject    Swordsman Swordsman;    @Override    protectedvoid  onCreate (Bundle savedinstancestate) {          Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_second);        App.get (secondactivity. this). Getactivitycomponent (). Inject (this);         = swordsman.fighting (;    }}

No7:

Lazy Loading: When the @inject is not initialized, but when used, call the Get method to get the instance

 Public classSecondactivityextendsappcompatactivity{Private Static FinalString tag= "Dagger2"; @Inject Lazy<Swordsman>Swordsmanlazy; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_second); App.get (secondactivity. This). Getactivitycomponent (). Inject ( This); Swordsman Swordsman=Swordsmanlazy.get ();        Swordsman.fighting (); String SD1=swordsman.fighting (); }}

"Android Advanced Light"--dagger2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.