Fix memory leaks reported by LeakCanary but ending infinite animations when Activity is paused (#2103)

This commit is contained in:
Joshua Soberg 2025-01-30 05:28:06 -05:00 committed by GitHub
parent b0fae9418c
commit 376c8b37b9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 28 additions and 8 deletions

View file

@ -397,4 +397,17 @@ class ArmoireActivity : BaseActivity() {
dialog.setContentView(R.layout.armoire_drop_rate_dialog)
dialog.show()
}
override fun onPause() {
super.onPause()
// Clear infinite animations on pause to make sure Context references aren't leaked.
stopInfiniteAnimations()
}
private fun stopInfiniteAnimations() {
binding.leftSparkView.stopAnimating()
binding.rightSparkView.stopAnimating()
binding.iconView.animation?.cancel()
binding.iconView.animation = null
}
}

View file

@ -27,6 +27,7 @@ constructor(
invalidate()
}
private var paint: Paint = Paint()
private var animator: ValueAnimator? = null
var thickness = 3.dpToPx(context)
var length = 6.dpToPx(context)
@ -59,15 +60,16 @@ constructor(
}
fun startAnimating() {
val anim = ObjectAnimator.ofFloat(thickness.toFloat(), maxSpacing.toFloat())
anim.addUpdateListener {
spacing = it.animatedValue as Float
animator = ObjectAnimator.ofFloat(thickness.toFloat(), maxSpacing.toFloat()).apply {
addUpdateListener {
spacing = it.animatedValue as Float
}
interpolator = AccelerateDecelerateInterpolator()
repeatCount = Animation.INFINITE
repeatMode = ValueAnimator.REVERSE
duration = animationDuration
start()
}
anim.interpolator = AccelerateDecelerateInterpolator()
anim.repeatCount = Animation.INFINITE
anim.repeatMode = ValueAnimator.REVERSE
anim.duration = animationDuration
anim.start()
}
override fun onMeasure(
@ -139,4 +141,9 @@ constructor(
paint
)
}
fun stopAnimating() {
animator?.end()
animator = null
}
}