zoukankan      html  css  js  c++  java
  • BaseActivity 基类代码

    public abstract class BaseActivity extends AppCompatActivity implements IBaseView {

    /**
    * 保存使用注解的 Presenter ,用于解绑
    */
    private List<BasePresenter> mInjectPresenters;

    protected abstract void initLayout(@Nullable Bundle savedInstanceState);

    protected abstract void initViews();

    protected abstract void initData();


    @SuppressWarnings("SameParameterValue")
    protected <T extends View> T $(@IdRes int viewId) {
    return findViewById(viewId);
    }

    @SuppressWarnings({"unchecked", "TryWithIdenticalCatches"})
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    initLayout(savedInstanceState);

    mInjectPresenters = new ArrayList<>();

    //获得已经申明的变量,包括私有的
    Field[] fields = this.getClass().getDeclaredFields();
    for (Field field : fields) {
    //获取变量上面的注解类型
    InjectPresenter injectPresenter = field.getAnnotation(InjectPresenter.class);
    if (injectPresenter != null) {
    try {
    Class<? extends BasePresenter> type = (Class<? extends BasePresenter>) field.getType();
    BasePresenter mInjectPresenter = type.newInstance();
    mInjectPresenter.attach(this);
    field.setAccessible(true);
    field.set(this, mInjectPresenter);
    mInjectPresenters.add(mInjectPresenter);
    } catch (IllegalAccessException e) {
    e.printStackTrace();
    } catch (InstantiationException e) {
    e.printStackTrace();
    }catch (ClassCastException e){
    e.printStackTrace();
    throw new RuntimeException("SubClass must extends Class:BasePresenter");
    }
    }
    }

    initViews();
    initData();
    }

    @Override
    protected void onDestroy() {
    super.onDestroy();
    /**
    * 解绑,避免内存泄漏
    */
    for (BasePresenter presenter : mInjectPresenters) {
    presenter.detach();
    }
    mInjectPresenters.clear(http://www.my516.com);
    mInjectPresenters = null;
    }

    @Override
    public Context getContext() {
    return this;
    }
    }
    ---------------------

  • 相关阅读:
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    Orge 安装
    vs番茄插件卸载,安装,破解
    Orge 学习资源
    3d开源引擎收集
    hashtable遍历
    java synchronized
    java 调用 ant
    用Ant实现Java项目的自动构建和部署
  • 原文地址:https://www.cnblogs.com/hyhy904/p/11299085.html
Copyright © 2011-2022 走看看