view.getViewTreeObserver().addOnGlobalLayoutListener 泄漏片段 [英] view.getViewTreeObserver().addOnGlobalLayoutListener leaks Fragment

查看:60
本文介绍了view.getViewTreeObserver().addOnGlobalLayoutListener 泄漏片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 GlobalLayoutListener 来查看 softKeyboard 是否打开时,片段被销毁后不再是垃圾收集.

When I use the GlobalLayoutListener to see if the softKeyboard is opened or not the fragment is not garbageCollected anymore after it is destroyed.

我做什么:

  • 我删除了 Fragment 的 onDestroy() 中的监听器
  • 我在 onDestroy()
  • 中将监听器设置为 null
  • 我在 onDestroy()
  • 中将观察到的视图设置为空
  • I remove the Listener in the onDestroy() of my Fragment
  • I set the Listener to null in onDestroy()
  • I set the view that is observed to null in onDestroy()

仍然泄漏片段.

有没有人遇到过类似的问题并且知道解决方法??

Does anyone had a similar issue and knows a fix for it??

我的onDestroy:

   @Override
public void onDestroy(){
    Log.d(TAG , "onDestroy");

    if(Build.VERSION.SDK_INT < 16){
        view.getViewTreeObserver().removeGlobalOnLayoutListener(gLayoutListener);
    }else{
        view.getViewTreeObserver().removeOnGlobalLayoutListener(gLayoutListener);
    }

    view = null;
    gLayoutListener = null;



    super.onDestroy();
    }

推荐答案

我认为在 onDestroy() 中强烈删除由 View 对象引用的监听器为时已晚.这个覆盖方法发生在 onDestroyView() 之后,它应该...清理与其视图关联的资源".您可以在 onStop() 中使用相同的代码.虽然我没有使用这个技巧.

I believe strongly removing the Listener, referenced by a View object, in onDestroy() is too late. This override method occurs after onDestroyView(), which supposed to "...clean up resources associated with its View." You may use the same code in onStop() instead. Although I did not use this technique.

我可以推荐这段代码,我已经使用过,调试器没有任何问题.

I can suggest this code, which I have used without any issues with the debugger.

// Code below is an example. Please change it to code that is more applicable to your app.
final View myView = rootView.findViewById(R.id.myView);
myView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @SuppressLint("NewApi") @SuppressWarnings("deprecation")
    @Override
    public void onGlobalLayout() {

        // Obtain layout data from view...
        int w = myView.getWidth();
        int h = myView.getHeight();
        // ...etc.

        // Once data has been obtained, this listener is no longer needed, so remove it...
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            myView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
        else {
            myView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        }
    }
});

注意事项:

  • 由于 getViewTreeObserver 用于布局,通常您只需要短时间使用此侦听器.因此立即删除侦听器.
  • 第二次调用 removeOnGlobalLayoutListener() 应该被 Studio 删除,因为它在 JELLY_BEAN 之前不可用.
  • 编译指示代码 @SuppressWarnings("deprecation") 如果您使用的是 Android Studio,则不需要.
  • 代码 myView = rootView.findViewById(R.id.myView); 可能需要更改为更适用于您的应用或情况的代码.
  • Since getViewTreeObserver is used for layouts, normally you need this listener for a short time only. Hence the listener is removed immediately.
  • The second call to removeOnGlobalLayoutListener() should be crossed out by the Studio since it is not available before JELLY_BEAN.
  • Pragma code @SuppressWarnings("deprecation") is not necessary if you're using Android Studio.
  • Code myView = rootView.findViewById(R.id.myView); may need to change to a more applicable code to your app or situation.

这篇关于view.getViewTreeObserver().addOnGlobalLayoutListener 泄漏片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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