博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
dagger2记录篇
阅读量:6271 次
发布时间:2019-06-22

本文共 3380 字,大约阅读时间需要 11 分钟。

作为一个码农,什么都不用多讲,贴代码

build project

build module

Application

public class App extends Application {    private AppComponent appComponent;    @Override public void onCreate() {        super.onCreate();        appComponent= DaggerAppComponent.builder()                .appModule(new AppModule(this))                .build();    }    public AppComponent getAppComponent() {        return appComponent;    }}
View Code
AppComponent
@Singleton@Component(modules = { AppModule.class })public interface AppComponent {    @ContextLife("Application")    App getApplication();    RetrofitHelper retrofitHelper();  //提供http的帮助类}
View Code
AppModule
@Modulepublic class AppModule {    private App application;    public AppModule(App application){        this.application=application;    }    @Provides    @Singleton    @ContextLife("Application")    public App provideApplication(){        return application;    }    @Provides    @Singleton    public RetrofitHelper provideRetrofitHelper() {        return new RetrofitHelper();    }}
View Code
ActivityComponent
@ActivityScope@Component(dependencies = AppComponent.class, modules = ActivityModule.class)//关联上下public interface ActivityComponent {    Activity getActivity();    void inject(LoginActivity loginActivity);//要在类中使用注入功能    void inject(MainActivity mainActivity);}
View Code
ActivityModule
@Modulepublic class ActivityModule {    private Activity mActivity;    public ActivityModule(Activity activity) {        this.mActivity = activity;    }    @Provides    @ActivityScope    public Activity provideActivity() {        return mActivity;    }    @Provides    @ActivityScope    public IPresenterFactory providePresenter(RetrofitHelper retrofitHelper) {        return new PresenterFactory(retrofitHelper);    }}
View Code

Base里面使用注入,否则无法注入

public abstract class PresenterActivity extends BaseActivity {    private final static String TAG = "PresenterActivity";    protected abstract void initInject();    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        initInject();    }    protected ActivityComponent getActivityComponent(){        return  DaggerActivityComponent.builder()                .appComponent(App.getInstance().getAppComponent())                .activityModule(getActivityModule())                .build();    }    private ActivityModule getActivityModule() {        return new ActivityModule(this);    }}
View Code

此时前提条件都有了

点击运行自动生成注入代码(没问题的前提)

使用注入:

public class MainActivity extends PresenterActivity {    @Inject    protected IPresenterFactory mPresenter;//没有创建对象,inject标签注入类型    private final int BASIC_PERMISSION_REQUEST_CODE = 100;    private TextView username;    @Override    public void findView() {        username = findView(R.id.username);        username.setText(App.getInstance().getUserEntity().getName()+" hello");    }    @Override    public void init() {        if (mPresenter != null){            Toasts.showShort("mPresenter success",context);//注入成功会打印出success        }    }    @Override    public int layoutResID() {        return R.layout.activity_main;    }    @Override    protected void initInject() {        getActivityComponent().inject(this);    }    @Subscribe(threadMode = ThreadMode.MAIN)    public void onEvent() {        Toasts.showShort("刷新UI",context);    }}

 

转载于:https://www.cnblogs.com/LiuZhen/p/5923387.html

你可能感兴趣的文章
nginx 基础文档
查看>>
LintCode: Unique Characters
查看>>
Jackson序列化和反序列化Json数据完整示例
查看>>
.net 中的DllImport
查看>>
nyoj 517 最小公倍数 【java睑板】
查看>>
include与jsp:include区别
查看>>
ftp的20 21端口和主动被动模式
查看>>
MySQL存储引擎选型
查看>>
Java中的statickeyword具体解释
查看>>
Linux车载系统的开发方向
查看>>
并发编程之五--ThreadLocal
查看>>
摄像头驱动OV7725学习笔记连载(二):0V7725 SCCB时序的实现之寄存器配置
查看>>
iOS播放短的音效
查看>>
[java] java 线程join方法详解
查看>>
JQuery datepicker 用法
查看>>
golang(2):beego 环境搭建
查看>>
天津政府应急系统之GIS一张图(arcgis api for flex)讲解(十)态势标绘模块
查看>>
程序员社交宝典
查看>>
ABP理论学习之MVC控制器(新增)
查看>>
Netty中的三种Reactor(反应堆)
查看>>