最近在看Dagger2,网上资料也很多,并且在自己私下的项目中Hot运用(一个关于微信热门头条的分享)。这里就不介绍dagger的一些使用方法啊,这里有个很好的介绍dagger介绍
DataManager管理
|
|
AppModule的构造的对象和提供的依赖
|
|
接下来的Component的注入的代码
ActivityComponent的代码
|
|
3.FragmentComponent的代码
之前在View 和P的关联在通过在p构造方法中传入,现在是使用调用p的attachView()方法。
现在BasePresenter设计
|
|
BaseActivity中的p之前需要自己new出来,现在加入了dagger2,通过注入的方式
/**
Created by wukewei on 16/5/26.
*/
public abstract class BaseActivityextends AppCompatActivity implements IView { @Inject
protected T mPresenter;
protected Activity mContext;@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState); setContentView(getLayout()); ButterKnife.bind(this); mContext = this; setupActivityComponent(App.getAppComponent(),new ActivityModule(this)); mPresenter.attachView(this); initEventAndData();
}
protected void setCommonBackToolBack(Toolbar toolbar, String title) {
toolbar.setTitle(title); setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayShowHomeEnabled(true); toolbar.setNavigationOnClickListener(v -> onBackPressed());
}
@Override
protected void onDestroy() {super.onDestroy(); ButterKnife.unbind(this); if (mPresenter != null) mPresenter.detachView();
}
/**
- 依赖注入的入口
@param appComponent appComponent
*/
protected abstract void setupActivityComponent(AppComponent appComponent, ActivityModule activityModule);protected abstract int getLayout();
protected abstract void initEventAndData();
}12多了setupActivityComponent()方法这就是依赖注入的入口好比。
@Override
protected void setupActivityComponent(AppComponent appComponent, ActivityModule activityModule) {DaggerActivityComponent.builder() .appComponent(appComponent) .activityModule(activityModule) .build() .inject(this);
}
````
总结
这个项目是我私下自己在学习新的技术,并且运用在一起的项目,也是像对待产品一个对待这个app开发,后续会不断的学习和更新项目。本人也是个android小菜鸟,从最弱最弱的做起。