package com.tejia.lijin.app.view;
|
|
import android.content.Context;
|
import android.os.Bundle;
|
import androidx.annotation.Nullable;
|
|
import com.tejia.lijin.app.ShoppingApplication;
|
import com.tejia.lijin.app.presenter.BasePresenter;
|
import com.tejia.lijin.app.ui.BaseFragmentActivity;
|
import com.tejia.lijin.app.util.ToolUtil;
|
|
|
/**
|
* 设置两个泛型——V和P,明显地,分别代表对应的View和Presenter
|
* 其持有一个BasePresenter,在onCreated方法中,使用createPresenter方法返回对应的BasePresenter的子类,我们就可以使用了。
|
* 这里注意一下view和presenter的处理:在onCreated中创建Presenter对象,但其内部的view软引用还是空;在onResume中关联view,此时presenter已经持有view的软引用;当然,还需要在onDestroy中取消关联。
|
*
|
* @param <V>
|
* @param <T>
|
*/
|
public abstract class BaseActivity<V, T extends BasePresenter<V>> extends BaseFragmentActivity {
|
public String TAG = getClass().getSimpleName() + "";
|
protected T mPresenter;
|
public Context mContext;
|
|
@Override
|
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
ToolUtil.setCustomDensity(this, ShoppingApplication.application);
|
super.onCreate(savedInstanceState);
|
initActivityView(savedInstanceState);
|
mContext = this;
|
//创建presenter
|
mPresenter = createPresenter();
|
// presenter与view绑定
|
if (null != mPresenter) {
|
mPresenter.attachView((V) this);
|
}
|
findViewById();
|
getData();
|
}
|
|
/**
|
* 关于Activity的界面填充的抽象方法,需要子类必须实现
|
*/
|
protected abstract void initActivityView(Bundle savedInstanceState);
|
|
/**
|
* 加载页面元素
|
*/
|
protected abstract void findViewById();
|
|
/**
|
* 创建Presenter 对象
|
*
|
* @return
|
*/
|
protected abstract T createPresenter();
|
|
protected abstract void getData();
|
|
@Override
|
protected void onDestroy() {
|
//销毁Presenter
|
if (null != mPresenter) {
|
mPresenter.detachView();
|
}
|
super.onDestroy();
|
}
|
|
}
|