上一篇中主要是将mvp+rxjava+retrofit进行了结合,本篇主要是对mvp框架的优化;建议先去看上一篇:Android 搭建MVP+Retrofit+RxJava网络请求框架(二)
- 针对view销毁时候产生的内存泄漏,对presenter持有view的引用进行优化:
public class BasePresenter<V extends BaseView> {
protected V mView;
protected Context context;
protected ApiService mApiService;
private CompositeDisposable mCompositeDisposable;
//使用弱引用
private Reference<V> mReference = null;
public BasePresenter(V v) {
this.mView = v;
initContext();
}
private void initContext() {
if (mView instanceof Fragment) {
context = ((Fragment) mView).getActivity();
} else {
context = (Context) mView;
}
}
//关联view
public void attachView(V mvpView) {
mReference = new WeakReference<V>(mvpView);
mApiService = RetrofitHelper.getInstance().getServer();
}
//判断是否关联
public boolean isAttach() {
return null != mReference && null != mReference.get();
}
//接触关联
public void detachView() {
if (null != mReference) {
mReference.clear();
mReference = null;
}
onUnsubscribe();
}
//RXjava取消注册,以避免内存泄露
public void onUnsubscribe() {
if (mCompositeDisposable != null && mCompositeDisposable.isDisposed()) {
mCompositeDisposable.clear();
}
}
public void addSubscription(Observable observable, DisposableObserver subscriber) {
if (mCompositeDisposable == null) {
mCompositeDisposable = new CompositeDisposable();
}
observable
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(subscriber);
mCompositeDisposable.add(subscriber);
}
}
在view进行销毁时候,接触关联,并且使用弱引用,可以优先被gc回收;
- 抽出一个MvpActivity 来简化项目,让所有使用mvp结构的avtivity继承这个类,在oncreate方法中关联view,在销毁的时候进行接触关联;
public abstract class MvpActivity<P extends BasePresenter> extends BaseActivity implements BaseView{
protected P mvpPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
mvpPresenter = createPresenter();
super.onCreate(savedInstanceState);
mvpPresenter.attachView(this);
}
protected abstract P createPresenter();
@Override
protected void onDestroy() {
super.onDestroy();
if (mvpPresenter != null) {
mvpPresenter.detachView();
}
}
public void showLoading() {
showProgressDialog();
}
public void hideLoading() {
dismissProgressDialog();
}
}
3.在mainActivity中继承MvpActivity
public class MainActivity extends MvpActivity<BookPresenter> implements BookInfoContract.IView {
@BindView(R.id.text)
TextView text;
@BindView(R.id.button)
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bindClick();
}
@Override
protected BookPresenter createPresenter() {
return new BookPresenter(this);
}
private void bindClick() {
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//开始请求
mvpPresenter.getMsg("人间失格", null, 0, 1);
// LogUtil.e("mvpPresenter","mvpPresenter执行了");
}
});
}
@Override
public void showError(String msg) {
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
@Override
public void showResult(String msg) {
text.setText(msg);
}
}
由于使用了retrofit+rxjava进行获取,暂时并未使用model,而是直接通过apiService进行获取Observable 后进行数据请求和回调处理;
github地址:https://github.com/cruiseliang/MvpSample