android--take you a little bit of encapsulation Project Mvp+baseactivity+retrofit+dagger+rxjava (iii)

Source: Internet
Author: User

1, This blog is with you to encapsulate our final Dagger2, in fact, before also wrote about the simple Dagger2, here is the address, completely did not understand the classmate can first go to see this, thank many small partners have been patiently waiting for this article

2, Dagger2 can be said to be the most difficult to get started with the technology, but the main four annotations in place, basically can be used:

@Inject Inject mainly has two functions, one is to use on the constructor, through the tag constructor to let the Dagger2 to use (Dagger2 through the Inject tag can be found when the class instance is needed to find the constructor and the relevant instance new out) to provide a dependency , another function is to tag the variables that need to be relied upon to give Dagger2 a dependency. @Provide use provide to annotate a method that can be called when a dependency is needed to assign a pre-supplied object as a dependency to a variable labeled @injection. Provide is mainly used to label the methods in the module @module the classes labeled with module are specifically designed to provide dependencies. Some people may have some doubts, look at the above @inject, need to be marked on the constructor to provide dependency, then if we need to provide the class constructor can not be modified, such as some of the class in the jar package, we can not modify the source code. This is the time to use the module. Module can provide the dependency of the class that cannot modify the source code, of course, can use the inject callout through module can also provide dependent @component Component commonly used to label the interface, The interface labeled component will generate an instance of the corresponding class at compile time as a bridge between providing the relying party and the relying party, and injecting the dependent dependency into it.

Here's a picture of you that sums up the effect of the above four tags.

There are also two common tags that are @scope and @qulifier, @Scope tags you can interpret it as a single-case marker, @Qulifier can be easily understood to distinguish between two or two construction methods (mainly used to differentiate).

Let's start with a simple scenario, now that our project uses the MVP architecture, and the main purpose of introducing Dagger2 is to solve our new presenter object in activity, first look at what we've been doing before referencing the code.

Package Com.qianmo.myview2;import Android.support.v7.app.appcompatactivity;import Android.os.bundle;import Com.qianmo.myview2.presenter.hipresenterimpl;public class Mainactivity extends Appcompatactivity {    private Hipresenterimpl Hipresenter;    @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity_main);        Direct New Object        Hipresenter = new Hipresenterimpl ();}    }   

  Quote Dagger2, and look at our code.

Mainactiivty.java

Package Com.qianmo.myview2;import Android.support.v7.app.appcompatactivity;import Android.os.bundle;import Com.qianmo.myview2.presenter.hipresenterimpl;import Javax.inject.inject;public class MainActivity extends appcompatactivity {    @Inject    private Hipresenterimpl hipresenter;  Here changed the     @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate ( Savedinstancestate);        Setcontentview (R.layout.activity_main);    }}

  Presenter.java

Package Com.qianmo.myview2.presenter;import Com.qianmo.myview2.contract.hicontract;import javax.inject.Inject;/** * Created by Mvphelper on 2016/10/20*/public class Hipresenterimpl implements hicontract.presenter{    @Inject    Public Hipresenterimpl () {    }}

  These two annotations on the light are not enough, we also need to create the corresponding module and component

Mainmodule.java

Package Com.qianmo.myview2.di.module;import Com.qianmo.myview2.presenter.hipresenterimpl;import Dagger. Module;import Dagger. provides;/** * Created by Wangjitao on 2017/3/9 0009. * E-mail:[email protected] */@Modulepublic class Mainmodule {    @Provides    Hipresenterimpl Providehipresenterimpl () {        return new Hipresenterimpl ();    }}

  Maincomponent.java

Package Com.qianmo.myview2;import Android.support.v7.app.appcompatactivity;import Android.os.bundle;import Com.qianmo.myview2.di.component.daggermaincomponent;import Com.qianmo.myview2.di.module.mainmodule;import Com.qianmo.myview2.presenter.hipresenterimpl;import Javax.inject.inject;public class MainActivity extends appcompatactivity {    @Inject    private Hipresenterimpl hipresenter;    @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity_main);        Daggermaincomponent.builder (). Mainmodule (New Mainmodule ()). Build (). inject (this);        Hipresenter.say ();    }}

  So we simply use, and then a simple analysis of the process: the Code @Inject Annotated there is a connection, when the code executes to the @Inject, the program will automatically enter into the constructor of this class, if it happens that this construction method is @Inject decorated, Then the system will automatically create objects for us.

3, package

We overdo it by the above items, but when we encapsulate it, we primarily want to hold presenter objects in activity and fragment, first to encapsulate the activity, which is the directory

First look at the creation of our previous presenter, add an abstract method in Baseactivity, override the method in Mainactivity, and then new object

  

the first thing to create the application module,application is to provide the context, and the component in Appliacation, just provide the contextual presentation method get method

Appmodule.java

Package Com.qianmo.mvpproject.di.module;import Com.qianmo.mvpproject.app;import Com.qianmo.mvpproject.di.contextlife;import Javax.inject.singleton;import Dagger. Module;import Dagger. provides;/** * Created by Wwangjitao on 16/8/7. */@Modulepublic class Appmodule {    private final App application;    Public Appmodule (App application) {        this.application = Application;    }    @Provides    @Singleton    @ContextLife ("Application")    App Provideapplicationcontext () {        return Application;}    }

  Appcomponent.java

Package Com.qianmo.mvpproject.di.component;import Com.qianmo.mvpproject.app;import Javax.inject.singleton;import Com.qianmo.mvpproject.di.contextlife;import Com.qianmo.mvpproject.di.module.appmodule;import Dagger. component;/** * Created by Wangjitao on 16/8/7. */@Singleton @component (modules = appmodule.class) public interface AppComponent {    @ContextLife ("Application")    App getcontext ();  Provide the app's context}

  The module of activity is mainly to provide activity and contextual context objects, the corresponding activity of the component is to inject the provided object into the place where it is needed.

Activitymodule.java

Package Com.qianmo.mvpproject.di.module;import Android.app.activity;import Android.content.context;import Com.qianmo.mvpproject.base.baserxpresenter;import Com.qianmo.mvpproject.di.activityscope;import Dagger. Module;import Dagger. provides;/** * Created by codeest on 16/8/7. */@Modulepublic class Activitymodule {    private Activity mactivity;    Public Activitymodule (activity activity) {        this.mactivity = activity;    }    @Provides    @ActivityScope public    Activity provideactivity () {        return mactivity;    }    @Provides public    Context Providecontext () {        return mactivity;    }}

  Activitycomponent.java

Package Com.qianmo.mvpproject.di.component;import Android.app.activity;import Com.qianmo.mvpproject.di.activityscope;import Com.qianmo.mvpproject.di.module.activitymodule;import Com.qianmo.mvpproject.ui.activity.checkversionactivity;import dagger.component;/** * Created by codeest on 16/8/7. */@ActivityScope @component (dependencies = appcomponent.class, modules = activitymodule.class) public interface activitycomponent {    Activity getactivity ();    void inject (checkversionactivity welcomeactivity);}

  Again in the baseactivity to call

Baseactivity.java

Package Com.qianmo.mvpproject.base;import Android.app.activity;import Android.os.bundle;import Android.support.annotation.nullable;import Android.support.v7.app.appcompatactivity;import Android.support.v7.widget.toolbar;import Javax.inject.inject;import Com.qianmo.mvpproject.app;import Com.qianmo.mvpproject.r;import Com.qianmo.mvpproject.di.component.activitycomponent;import Com.qianmo.mvpproject.di.component.daggeractivitycomponent;import Com.qianmo.mvpproject.di.module.activitymodule;import Butterknife. Butterknife;import Butterknife. unbinder;/** * Created by Wangjitao on 2016/11/8 0008. * Package of base activity * Generally using MVP mode, the initialization binding of P and V is performed in baseactivity */public abstract class Baseactivity<t extends Basepresenter    > extends Appcompatactivity implements Baseview {//Here added injected @Inject protected T mpresenter; ....... Omit protected void OnCreate (@Nullable Bundle savedinstancestate) {....        Omit Super.oncreate (savedinstancestate);       Setcontentview (GetLayout ());         The injected call Initinject () is added here;           if (mpresenter! = null) Mpresenter.attachview (this);                }//Add Call Method protected Activitycomponent Getactivitycomponent () {return Daggeractivitycomponent.builder () . AppComponent (App.getappcomponent ()). Activitymodule (Getactivitymodule ()). Buil    D ();    } protected Activitymodule Getactivitymodule () {return new activitymodule (this); }}

  OK, so we encapsulated, the same fragment, here is not nonsense to write, the following GitHub address affixed, behind the plan to write a display class app, but also everywhere to find the interface, see you Next time!

android--take you a little bit of encapsulation Project Mvp+baseactivity+retrofit+dagger+rxjava (iii)

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.