Skip to content

Commit

Permalink
Fix memory leaks reported by LeakCanary but ending infinite animation…
Browse files Browse the repository at this point in the history
…s when Activity is paused (#2103)
  • Loading branch information
jsoberg authored Jan 30, 2025
1 parent b0fae94 commit 376c8b3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -139,4 +141,9 @@ constructor(
paint
)
}

fun stopAnimating() {
animator?.end()
animator = null
}
}

0 comments on commit 376c8b3

Please sign in to comment.