上一篇中主要是將mvp+rxjava+retrofit進(jìn)行了結(jié)合抒和,本篇主要是對(duì)mvp框架的優(yōu)化版保;建議先去看上一篇:Android 搭建MVP+Retrofit+RxJava網(wǎng)絡(luò)請(qǐng)求框架(二)
- 針對(duì)view銷毀時(shí)候產(chǎn)生的內(nèi)存泄漏杖们,對(duì)presenter持有view的引用進(jìn)行優(yōu)化:
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;
}
}
//關(guān)聯(lián)view
public void attachView(V mvpView) {
mReference = new WeakReference<V>(mvpView);
mApiService = RetrofitHelper.getInstance().getServer();
}
//判斷是否關(guān)聯(lián)
public boolean isAttach() {
return null != mReference && null != mReference.get();
}
//接觸關(guān)聯(lián)
public void detachView() {
if (null != mReference) {
mReference.clear();
mReference = null;
}
onUnsubscribe();
}
//RXjava取消注冊(cè)画饥,以避免內(nèi)存泄露
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進(jìn)行銷毀時(shí)候蚂斤,接觸關(guān)聯(lián),并且使用弱引用畔派,可以優(yōu)先被gc回收铅碍;
- 抽出一個(gè)MvpActivity 來(lái)簡(jiǎn)化項(xiàng)目,讓所有使用mvp結(jié)構(gòu)的avtivity繼承這個(gè)類线椰,在oncreate方法中關(guān)聯(lián)view,在銷毀的時(shí)候進(jìn)行接觸關(guān)聯(lián)胞谈;
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) {
//開(kāi)始請(qǐng)求
mvpPresenter.getMsg("人間失格", null, 0, 1);
// LogUtil.e("mvpPresenter","mvpPresenter執(zhí)行了");
}
});
}
@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進(jìn)行獲取,暫時(shí)并未使用model,而是直接通過(guò)apiService進(jìn)行獲取Observable 后進(jìn)行數(shù)據(jù)請(qǐng)求和回調(diào)處理憨愉;
github地址:https://github.com/cruiseliang/MvpSample