android内存泄露

首先说下场景是如何的。使用的是xUtils这个网络访问框架,HttpLoader是一个单例。


</pre><pre code_snippet_id="1594391" snippet_file_name="blog_20160302_2_394753" name="code" class="java">private void checkUpdate(final boolean showUpdateDialog) {
        HttpLoader.getInstance(AboutActivityDump.this).submit(AboutActivityDump.this, new UpdateFileInfo(AboutActivityDump.this), UpdateFileDataBean.class, new HttpBaseInfo.OnResponseListener() {
            @Override
            public void onHttpSuccess(Object httpBeanBase) {
                UpdateFileDataBean updateFileDataBean = (UpdateFileDataBean) httpBeanBase;
                if (updateFileDataBean != null) {
                    UpdateFileBean updateFileBean = updateFileDataBean.getResultData();

                    String tips;
                    // 有更新
                    if (updateFileBean != null && (TextUtils.equals(UpdateFileBean.FORCE_UPDATE, updateFileBean.getType()) || TextUtils.equals(UpdateFileBean.NORMAL_UPDATE, updateFileBean.getType()
                    ))) {
                        if (showUpdateDialog) {
                            UpdateDialogActivity.start(AboutActivityDump.this, updateFileBean);
                        }
                        tips = getString(R.string.have_update);
                    } else {
                        tips = getString(R.string.about_already_lastest_version);
                        if (showUpdateDialog) {
                            ToastTool.getInstance(AboutActivityDump.this).show(tips);
                        }
                    }
                    mCheckUpdateTipsTextView.setText(tips);
                }
            }

            @Override
            public void onHttpFailed(HttpBeanBase httpBeanBase) {
            }
        }, false);

    }

这里使用的是new HttpBaseInfo.OnResponseListener(){}这个匿名内部类。

而它持有了Activity的引用,所以导致了内存泄露了,那么将其改为public static class。


但在改为public static class的过程中,发现这里的MyResponseListener操作了,AboutActivity中的控件,还是需要将AboutActivity传递过去。

这个时候持需要使用若引用WeakReference来实现这个才可以,代码如下。

private void checkUpdate(final boolean showUpdateDialog) {
        HttpLoader.getInstance(AboutActivity.this).submit(AboutActivity.this, new UpdateFileInfo(AboutActivity.this), UpdateFileDataBean.class, new MyResponseListener(this, showUpdateDialog), false);
    }

    public static class MyResponseListener implements HttpBaseInfo.OnResponseListener {
        private Context context;
        private WeakReference<AboutActivity> mActivityReference;
        private boolean showUpdateDialog;

        public MyResponseListener(AboutActivity activity, boolean showUpdateDialog) {
            this.context = activity.getApplicationContext();
            this.mActivityReference = new WeakReference<AboutActivity>(activity);
            this.showUpdateDialog = showUpdateDialog;
        }
        @Override
        public void onHttpSuccess(Object httpBeanBase) {
            UpdateFileDataBean updateFileDataBean = (UpdateFileDataBean) httpBeanBase;
            if (updateFileDataBean != null) {
                UpdateFileBean updateFileBean = updateFileDataBean.getResultData();
                String tips;
                // 有更新
                if ((updateFileBean != null) &&
                   (TextUtils.equals(UpdateFileBean.FORCE_UPDATE, updateFileBean.getType()) ||
                    TextUtils.equals(UpdateFileBean.NORMAL_UPDATE, updateFileBean.getType()))) {

                    if (showUpdateDialog) {
                        UpdateDialogActivity.start(context, updateFileBean);
                    }
                    tips = context.getResources().getString(R.string.have_update);
                } else {
                    tips = context.getResources().getString(R.string.about_already_lastest_version);
                    if (showUpdateDialog) {
                        ToastTool.getInstance(context).show(tips);
                    }
                }

                final AboutActivity activity = mActivityReference.get();
                if (activity != null) {
                    activity.mCheckUpdateTipsTextView.setText(tips);
                }
            }
        }

        @Override
        public void onHttpFailed(HttpBeanBase httpBeanBase) {
        }
    }




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值