2)我们不用再在每个Activity中手动调用这三个方法,因为我们的BaseActivity已经为我们做好了封装,只要重写这三个抽象方法就好了就好了
代码如下:Dagger2+BufferKnife
package com.example.administrator.myapplication.Base;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
import com.example.administrator.myapplication.dagger.component.ActivityComponent;
import com.example.administrator.myapplication.dagger.component.DaggerActivityComponent;
import com.example.administrator.myapplication.dagger.module.ActivityModule;
import butterknife.ButterKnife;
/**
* Created on 2017/5/10.
* Author:crs
* Description:BaseActivity抽象父类
*/
public abstract class BaseActivity extends AppCompatActivity {
private static final String TAG = "BaseActivity";
public Context mContext;
ActivityComponent mActivityComponent;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mActivityComponent = DaggerActivityComponent.builder().activityModule(new ActivityModule(this)).build();
mContext = this;
setContentView();
//ButterKnife,this指代子Activity
ButterKnife.bind(this);
//Log.i(TAG,this+""); MainActivity@53538dd8
//Dagger2
initInject();
init();
}
//依赖注入
public abstract void initInject();
//设置布局
public abstract void setContentView();
//初始化控件
public abstract void init();
//通用性方法
public void showToast(String msg) {
Toast.makeText(BaseActivity.this, msg, Toast.LENGTH_SHORT).show();
}
//子Activity通过此ActivityComponent进行依赖注入
public ActivityComponent getActivityComponent() {
return mActivityComponent;
}
}
MainActivity中的代码:
package com.example.administrator.myapplication;
import android.widget.TextView;
import com.example.administrator.myapplication.Base.BaseActivity;
import butterknife.BindView;
import butterknife.OnClick;
public class MainActivity extends BaseActivity {
//此方法已经不需要,调用默认的就可以了。
//@Override
//protected void onCreate(Bundle savedInstanceState) {
// super.onCreate(savedInstanceState);
// ButterKnife.bind(this);
//}
@BindView(R.id.tv_content)
TextView tv_content;
@Override
public void initInject() {
//注入当前Activity,mActivityComponent()封装在父类中;
//ActivityComponent文件中:连接器
//ActivityModule:主要用于提供presenter类的对象,方法有参数,是通过构造函数提供的对象。
getActivityComponent().inject(this);
}
@Override
public void setContentView() {
setContentView(R.layout.activity_main);
}
@Override
public void init() {
}
@OnClick(R.id.btn)
public void loadData(){
}
}
ActivityComponent中的代码:
package com.example.administrator.myapplication.dagger.component;
;
import com.example.administrator.myapplication.MainActivity;
import com.example.administrator.myapplication.dagger.module.ActivityModule;
import dagger.Component;
/**
* Created on 2017/5/10.
* Author:crs
* Description:ActivityComponent连接器
*/
@Component(modules = {ActivityModule.class})
public interface ActivityComponent {
//注入MainActivity
void inject(MainActivity activity);
}
ActivityModule中的代码:
package com.example.administrator.myapplication.dagger.module;
import android.app.Activity;
import dagger.Module;
import dagger.Provides;
/**
* Created on 2017/5/10.
* Author:crs
* Description:ActivityModule,自定义module类
*/
@Module
public class ActivityModule {
private Activity mActivity;
public ActivityModule(Activity activity) {
this.mActivity = activity;
}
@Provides
public Activity provideActivity() {
return mActivity;
}
}
//添加日志拦截器:看看效果
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
okHttpClientBuilder.addInterceptor(interceptor);
添加依赖的方式,而非导入jar包,否则可能导致方法数超过65535,导致apk过大。
对项目依赖的管理:
compile 'com.alibaba:fastjson:1.2.37'
compile 'com.squareup.okhttp3:okhttp:3.3.1'
compile 'com.squareup.okhttp3:logging-interceptor:3.3.1'
compile 'com.squareup.okio:okio:1.13.0'
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
类头注释:
/**
* Created on ${DATE}.
* Author:crs
* Description:
*/
项目里面已经使用了Bufferknife所以就不需要在使用
setOnClistenerListener(this) LoginAct里面有很多冗余的代码(版本维护的时候进行删除)
在父类里面封装了加载对话框以及隐藏对话框的操作。
项目里的初始化方法:父类BaseActivity里面的抽象方法
getLayoutId()
initParams()