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

【内存泄漏】 ShimmerLayout #addUpdateListener #60

Open
Stars-FC opened this issue Jul 2, 2021 · 0 comments
Open

【内存泄漏】 ShimmerLayout #addUpdateListener #60

Stars-FC opened this issue Jul 2, 2021 · 0 comments

Comments

@Stars-FC
Copy link

Stars-FC commented Jul 2, 2021

该库已经不维护了,遇到问题很难搞。分享下遇到内存泄漏问题、产生原因及解决办法。
(由 https://github.com/team-supercharge/ShimmerLayout 库产生的内存泄漏)

内存泄漏的具体路径:
image

场景:RecyclerView + Skeleton + 弹起的软键盘(这个很重要)
原因:
1、SkeletonAdapter 中开启了ShimmerLayout动画,交由ShimmerLayout的onDetachedFromWindow()方法自己去移除动画监听。但在软键盘弹起情况下,被软键盘盖住View的onDetachedFromWindow()方法会在onAttachedToWindow()方法后立即回调,导致该View的动画监听没有被移除,进而导致泄漏。

解决思路:
1、SkeletonAdapter中使用了ShimmerLayout.startShimmerAnimation()方法,却没有手动调用 stopShimmerAnimation方法来移除相关监听。
2、ShimmerLayout 中如果已经回调了onDetachedFromWindow,就不需要再为他启动动画。

` /**
* 判断是否执行过{@link #onDetachedFromWindow()}方法
*/
private boolean isDetachedFromWindow;

@Override
protected void onAttachedToWindow() {
    super.onAttachedToWindow();
    isDetachedFromWindow = false;
}

@Override
protected void onDetachedFromWindow() {
    isDetachedFromWindow = true;
    resetShimmering();
    super.onDetachedFromWindow();
}

public void startShimmerAnimation() {
    if (isDetachedFromWindow) {
        //若执行过onDetachedFromWindow()方法,则不执行后后续逻辑
        return;
    }
}`

我采用了思路2来解决该问题(可能不是最好的解决方式,但解决了我的问题)。
建议:思路1中提到的,调用了startShimmerAnimation应该主动去调用stopShimmerAnimation方法来清除相关监听,而不是只交给View自身去处理,因为你不能确保View内会不会处理好。

————————————————————
如果你的项目还在使用该库并遇到该问题,希望能帮到你。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant