• BaseActivity 代码



    package com.test.mvp.mvpdemo.mvp.v5.basemvp;

    import android.content.Context;
    import android.os.Bundle;
    import android.support.annotation.IdRes;
    import android.support.annotation.Nullable;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;

    import com.test.mvp.mvpdemo.mvp.v5.inject.InjectPresenter;

    import java.lang.reflect.Field;
    import java.util.ArrayList;
    import java.util.List;

    public abstract class BaseActivity<P extends IBasePresenter> extends AppCompatActivity implements IBaseView {

    private P mPresenter;

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

    protected abstract void initLayout(@Nullable Bundle savedInstanceState);

    protected abstract P setPresenter();

    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);

    /**
    * 实例化和绑定 P 层
    */
    this.mPresenter = setPresenter();
    if (mPresenter != null) {
    this.mPresenter.attach(this);
    }

    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();
    /**
    * 解绑,避免内存泄漏
    */
    this.mPresenter.detach();
    this.mPresenter = null;
    for (BasePresenter presenter : mInjectPresenters) {
    presenter.detach();
    }
    mInjectPresenters.clear();
    mInjectPresenters = null;
    }

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

    public P getPresenter(http://www.my516.com) {
    return mPresenter;
    }
    }
    --------------------- 

  • 相关阅读:
    Unity3D研究院之Assetbundle的实战(六十三)
    Unity3D研究院之Assetbundle的原理(六十一)
    常见图片格式详解
    unity 查看打包资源占用
    MUI框架-04-切换页面头部文字重叠
    MUI框架-03-自定义MUI控件样式
    MUI框架-02-注意事项-适用场景-实现页面间传值
    MUI框架-01-介绍-创建项目-简单页面
    安卓app开发-05-Android xml布局详细介绍
    安卓app开发-04- app运行的运行和调试
  • 原文地址:https://www.cnblogs.com/hyhy904/p/11299067.html
Copyright © 2020-2023  润新知