Skip to content

Commit 376c8b3

Browse files
authored
Fix memory leaks reported by LeakCanary but ending infinite animations when Activity is paused (#2103)
1 parent b0fae94 commit 376c8b3

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

Habitica/src/main/java/com/habitrpg/android/habitica/ui/activities/ArmoireActivity.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,4 +397,17 @@ class ArmoireActivity : BaseActivity() {
397397
dialog.setContentView(R.layout.armoire_drop_rate_dialog)
398398
dialog.show()
399399
}
400+
401+
override fun onPause() {
402+
super.onPause()
403+
// Clear infinite animations on pause to make sure Context references aren't leaked.
404+
stopInfiniteAnimations()
405+
}
406+
407+
private fun stopInfiniteAnimations() {
408+
binding.leftSparkView.stopAnimating()
409+
binding.rightSparkView.stopAnimating()
410+
binding.iconView.animation?.cancel()
411+
binding.iconView.animation = null
412+
}
400413
}

Habitica/src/main/java/com/habitrpg/android/habitica/ui/views/SparkView.kt

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ constructor(
2727
invalidate()
2828
}
2929
private var paint: Paint = Paint()
30+
private var animator: ValueAnimator? = null
3031

3132
var thickness = 3.dpToPx(context)
3233
var length = 6.dpToPx(context)
@@ -59,15 +60,16 @@ constructor(
5960
}
6061

6162
fun startAnimating() {
62-
val anim = ObjectAnimator.ofFloat(thickness.toFloat(), maxSpacing.toFloat())
63-
anim.addUpdateListener {
64-
spacing = it.animatedValue as Float
63+
animator = ObjectAnimator.ofFloat(thickness.toFloat(), maxSpacing.toFloat()).apply {
64+
addUpdateListener {
65+
spacing = it.animatedValue as Float
66+
}
67+
interpolator = AccelerateDecelerateInterpolator()
68+
repeatCount = Animation.INFINITE
69+
repeatMode = ValueAnimator.REVERSE
70+
duration = animationDuration
71+
start()
6572
}
66-
anim.interpolator = AccelerateDecelerateInterpolator()
67-
anim.repeatCount = Animation.INFINITE
68-
anim.repeatMode = ValueAnimator.REVERSE
69-
anim.duration = animationDuration
70-
anim.start()
7173
}
7274

7375
override fun onMeasure(
@@ -139,4 +141,9 @@ constructor(
139141
paint
140142
)
141143
}
144+
145+
fun stopAnimating() {
146+
animator?.end()
147+
animator = null
148+
}
142149
}

0 commit comments

Comments
 (0)