@@ -32,6 +32,7 @@ import android.view.MenuInflater
32
32
import android.view.MenuItem
33
33
import android.view.View
34
34
import android.view.ViewGroup
35
+ import android.widget.Button
35
36
import androidx.annotation.Nullable
36
37
import androidx.appcompat.app.AlertDialog
37
38
import androidx.appcompat.content.res.AppCompatResources
@@ -151,7 +152,7 @@ class FeedFragment : BaseStateFragment<FeedState>() {
151
152
! recyclerView.canScrollVertically(- 1 )
152
153
) {
153
154
154
- if (feedBinding.newItemsLoadedButton. isVisible) {
155
+ if (tryGetNewItemsLoadedButton()?. isVisible == true ) {
155
156
hideNewItemsLoaded(true )
156
157
}
157
158
}
@@ -263,6 +264,9 @@ class FeedFragment : BaseStateFragment<FeedState>() {
263
264
}
264
265
265
266
override fun onDestroyView () {
267
+ // Ensure that all animations are canceled
268
+ feedBinding.newItemsLoadedButton?.clearAnimation()
269
+
266
270
feedBinding.itemsList.adapter = null
267
271
_feedBinding = null
268
272
super .onDestroyView()
@@ -624,9 +628,9 @@ class FeedFragment : BaseStateFragment<FeedState>() {
624
628
}
625
629
626
630
private fun showNewItemsLoaded () {
627
- feedBinding.newItemsLoadedButton .clearAnimation()
628
- feedBinding.newItemsLoadedButton
629
- .slideUp(
631
+ tryGetNewItemsLoadedButton()? .clearAnimation()
632
+ tryGetNewItemsLoadedButton()
633
+ ? .slideUp(
630
634
250L ,
631
635
delay = 100 ,
632
636
execOnEnd = {
@@ -641,23 +645,32 @@ class FeedFragment : BaseStateFragment<FeedState>() {
641
645
}
642
646
643
647
private fun hideNewItemsLoaded (animate : Boolean , delay : Long = 0) {
644
- feedBinding.newItemsLoadedButton .clearAnimation()
648
+ tryGetNewItemsLoadedButton()? .clearAnimation()
645
649
if (animate) {
646
- feedBinding.newItemsLoadedButton .animate(
650
+ tryGetNewItemsLoadedButton()? .animate(
647
651
false ,
648
652
200 ,
649
653
delay = delay,
650
654
execOnEnd = {
651
655
// Make the layout invisible so that the onScroll toTop method
652
656
// only does necessary work
653
- feedBinding?.newItemsLoadedButton ?.isVisible = false
657
+ tryGetNewItemsLoadedButton() ?.isVisible = false
654
658
}
655
659
)
656
660
} else {
657
- feedBinding.newItemsLoadedButton .isVisible = false
661
+ tryGetNewItemsLoadedButton()? .isVisible = false
658
662
}
659
663
}
660
664
665
+ /* *
666
+ * The view/button can be disposed/set to null under certain circumstances.
667
+ * E.g. when the animation is still in progress but the view got destroyed.
668
+ * This method is a helper for such states and can be used in affected code blocks.
669
+ */
670
+ private fun tryGetNewItemsLoadedButton (): Button ? {
671
+ return _feedBinding ?.newItemsLoadedButton
672
+ }
673
+
661
674
// /////////////////////////////////////////////////////////////////////////
662
675
// Load Service Handling
663
676
// /////////////////////////////////////////////////////////////////////////
0 commit comments