• MVP-1:使用弱引用和rxjava解决内存泄漏问题


    一.Model层封装

    1.RetrofitManager:懒汉双重锁

    public class RetrofitManager {
        //单例模式:懒汉双重锁校验 节省资源
        private RetrofitManager(){}
        private static RetrofitManager sRetrofitManager;//区分成员变量m 静态变量s 局部变量
        public static RetrofitManager getInstance(){
            if(sRetrofitManager == null){
                synchronized (RetrofitManager.class){
                    if(sRetrofitManager == null){
                        sRetrofitManager = new RetrofitManager();
                    }
                }
            }
            return sRetrofitManager;
        }
    
        private Retrofit mRetrofit;
        public Retrofit getRetrofit() {
            if(mRetrofit == null){
               createRetrofit();
            }
            return mRetrofit;
        }
        //构建Retrofit对象
        private void createRetrofit() {
            mRetrofit = new Retrofit.Builder()
                    .baseUrl("http://82.156.173.100:8087")
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .build();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    2.ApiServer

    public interface ApiServer {
        //分页加载
        @GET("video/findVideos?pageSize=200")
        Observable<VideoEntity> video(@Query("currentPage") int currentPage);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    3.BaseModel

    //网络请求
    public class BaseModel {
        //网络请求接口文档 private:只能被本类访问  protected:被本类以及实现子类访问
        protected ApiServer mApiServer;
        public BaseModel() {
            mApiServer = RetrofitManager.getInstance().getRetrofit().create(ApiServer.class);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    二.Presenter层封装

    //绑定解析View 以图和数据交互
    public class BasePresenter<V> {
        //用弱引用创建View
    
        //弱引用 ->解决MVP的内存泄露->OOM内存溢出
        //强 软 弱 虚
        WeakReference<V> mWeakReference;
        //请求方式 okHttp+retrofit+rxjava
        //每个请求 都会产生订阅->把订阅做一个统一的管理
    
        //将rxjava的 订阅 添加到mCompositeDisposable 通过mCompositeDisposable 对订阅的请求做统一的管理
        protected CompositeDisposable mCompositeDisposable = new CompositeDisposable();
    
    //    通过mWeakReference创建View
        public void attachView(V view){
            mWeakReference = new WeakReference<V>(view);
        }
        //取出对应的view
        protected V getView(){
            return mWeakReference == null?null:mWeakReference.get();
        }
        //在咱们取出对应的view之前做这个判断
        protected boolean isViewAttached(){
            return mWeakReference != null && mWeakReference.get() !=null;
        }
    
        //在APP或者页面的生命周期结束 及其他需要的地方 释放View 取消订阅
    
        public void detachView(){
    //        释放View
            if (mWeakReference != null){
                mWeakReference.clear();
                mWeakReference = null;
            }
    //        取消订阅
            mCompositeDisposable.clear();
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    三.View层封装

    1.IBaseView

    //展示 view最基础的能力
    public interface IBaseView {
        //加载中
        void onLoading();
        //加载成功
        void onLoadSuccess();
        //加载失败
        void onLoadFailed();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    2.BaseActivity

    public abstract   class BaseActivity<V,P extends BasePresenter<V>> extends AppCompatActivity implements IBaseView{
        protected  P mPresenter;
        //初始化数据:创建mPresenter
        protected abstract void initData();
        //初始化
        protected abstract void initView(Bundle savedInstanceState);
        //布局id
        protected abstract int bindLayout();
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(bindLayout());
            initView(savedInstanceState);
            initData();
            //使用弱引用创建View
            if(mPresenter != null){
                mPresenter.attachView((V)this);
            }
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            //释放view 取消订阅
            if(mPresenter != null){
                mPresenter.detachView();
            }
        }
    
        @Override
        public void onLoading() {
            Toast.makeText(this, "数据加载中", Toast.LENGTH_SHORT).show();
        }
    
        @Override
        public void onLoadSuccess() {
            Toast.makeText(this, "数据加载成功", Toast.LENGTH_SHORT).show();
        }
    
        @Override
        public void onLoadFailed() {
            Toast.makeText(this, "数据加载失败", Toast.LENGTH_SHORT).show();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    3.BaseFragment

    public abstract class BaseFragment<V,P extends BasePresenter<V>> extends Fragment implements IBaseView {
        protected  P mPresenter;
        protected abstract void initData();
        protected abstract void initView();
        protected abstract int bindLayout();
        protected  View view;
    
    
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            view = inflater.inflate(bindLayout(),container,false);
            initView();
            initData();
            //使用弱引用创建View
            if(mPresenter != null){
                mPresenter.attachView((V)this);
            }
            return view;
        }
        @Override
        public void onDestroy() {
            super.onDestroy();
            if (mPresenter != null){
                //释放订阅 销毁view
                mPresenter.detachView();
            }
        }
        //多的方法:findViewById
        public <T extends View> T findViewById(@IdRes int id) {
            return mView.findViewById(id);
        }
    
        @Override
        public void onLoading() {
            Toast.makeText(getContext(), "数据加载中", Toast.LENGTH_SHORT).show();
        }
    
        @Override
        public void onLoadSuccess() {
            Toast.makeText(getContext(), "数据加载成功", Toast.LENGTH_SHORT).show();
        }
    
        @Override
        public void onLoadFailed() {
            Toast.makeText(getContext(), "数据加载失败", Toast.LENGTH_SHORT).show();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
  • 相关阅读:
    [巅峰极客 2022]smallcontainer
    【Spring Boot】web开发相关源码分析
    javaDoc文档注释,在线文档生成,基础详解,idea加载生成doc文档
    PyQt5使用sqlite3库存用户数据
    react合成事件——bind解决this指向——箭头函数解决this指向
    使用mybatis标签规避空where
    资料误删也不怕,这有恢复妙招快来看
    安卓毕业设计app项目源码移动端的医生寻访平台
    矩阵分析与应用-4.7-QR分解及其应用
    车规级MCU进入「新周期」,中国本土供应商竞逐竞争力TOP10
  • 原文地址:https://blog.csdn.net/qq_34178710/article/details/126242284