Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BasePopupWindow的内存泄漏问题 #484

Open
hotdl opened this issue Feb 15, 2023 · 1 comment
Open

BasePopupWindow的内存泄漏问题 #484

hotdl opened this issue Feb 15, 2023 · 1 comment
Labels

Comments

@hotdl
Copy link

hotdl commented Feb 15, 2023

  • 库版本(必须)/ Library version (required):3.2.0
  • 问题代码/截图(可选)/ Problem code or screenshot (optional):
  • private void listenForLifeCycle(Activity act) {
     act.getWindow()
             .getDecorView()
             .addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
                 @Override
                 public void onViewAttachedToWindow(View v) {
    
                 }
    
                 @Override
                 public void onViewDetachedFromWindow(View v) {
                     v.removeOnAttachStateChangeListener(this);
                     onDestroy();
                 }
             });
    
    }

问题描述/重现步骤请写在这里
Please write the description here.

在非LifecycleOwner的Activity下使用BasePopupWindow,在Activity销毁前,popupWindow调用onDestroy会导致内存泄露问题
发现根源是在非LifecycleOwner的Activity下使用BasePopupWindow,会执行到listenForLifeCycle方法,该方法里使用了给decorView添加了一个listener,然后这个listener持有basePopupWindow的引用,只要activity不销毁,这个popupWindow就一直存在内存中,即使调用了onDestroy方法。

@razerdp
Copy link
Owner

razerdp commented Feb 22, 2023

一般来说,popup都是跟随act的生命期的。
相同的popup不建议一直new哈,可以随时改变里面的内容。

不过如果提前想destroy的话确实没有移除listener,这个可以修复一下。不过如果手动destroy,则下次show之前需要自己重新new一个哦。

除非不保存成员变量,直接new xxx.show()

@razerdp razerdp added the bug label Mar 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants