Android内部类内存泄漏和上下文泄漏? [英] Android inner classes memory leak and leak by context?

查看:195
本文介绍了Android内部类内存泄漏和上下文泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在启动画面中使用Handler将重定向推迟到下一个活动,如下所示..

I am using Handler in my splash screen for delaying redirection to the next activity as follows..

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.entrance);
        screenTimeOut();
    }

    private void screenTimeOut() {
          /* New Handler to start the next screen
         * and close this Entrance after some seconds.*/
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                initTracker();
                /* Create an Intent that will start the Next-Activity. */
                checkLoginStatus();
            }
        }, SPLASH_DISPLAY_LENGTH);
    }

在另一个活动中,我将上下文传递给一个类,并将上下文保存在如下 按钮点击 ..

And in another activity am passing context to a class and holding the context inside that as follows on button click ..

 private Tools tools;

 tools = new Tools(DetailsScreen.this, true);

工具

    private Context _context;
    private Fragment _fragment;
    private Activity activity;
    private String filePath = null;
    private String camImagePath = null;

    public Tools() {
    }

    public Tools(Context _context, boolean flag) {
        this._context = _context;
        this.activity = (Activity) _context;
        initPath();
        if (flag) {
            createImageFile();
        }
    }

其中任何一个都是泄漏的原因?

Is any one of these will be a reason for leakage ?

如何使用处理程序如下..

And how about using handler as follows..

 private Handler mHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.entrance);
        screenTimeOut();
    }

 private void screenTimeOut() {
          /* New Handler to start the next screen
         * and close this Entrance after some seconds.*/
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                initTracker();
                /* Create an Intent that will start the Next-Activity. */
                checkLoginStatus();
            }
        }, SPLASH_DISPLAY_LENGTH);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHandler.removeCallbacksAndMessages(null);
    }


推荐答案


  1. 处理程序 Runnable 不应以匿名方式使用。

  2. 你应避免将整个活动作为参数传递给其他类。如果上下文是您想要的全部 activity.getContext()

  1. Handler and Runnable should not be used in anonymous fashion.
  2. You should avoid passing the whole activity as a parameter to other classes. If context is all that you want use activity.getContext().

更多信息:
http:// www。 androiddesignpatterns.com/2013/01/inner-class-handler-memory-leak.html
http://www.androiddesignpatterns.com/2013/04/activitys-threads-memory-leaks.html

这篇关于Android内部类内存泄漏和上下文泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆