@@ -32,6 +32,7 @@ import android.view.MenuInflater
3232import android.view.MenuItem
3333import android.view.View
3434import android.view.ViewGroup
35+ import android.widget.Button
3536import androidx.annotation.Nullable
3637import androidx.appcompat.app.AlertDialog
3738import androidx.appcompat.content.res.AppCompatResources
@@ -151,7 +152,7 @@ class FeedFragment : BaseStateFragment<FeedState>() {
151152 ! recyclerView.canScrollVertically(- 1 )
152153 ) {
153154
154- if (feedBinding.newItemsLoadedButton. isVisible) {
155+ if (tryGetNewItemsLoadedButton()?. isVisible == true ) {
155156 hideNewItemsLoaded(true )
156157 }
157158 }
@@ -264,6 +265,9 @@ class FeedFragment : BaseStateFragment<FeedState>() {
264265 }
265266
266267 override fun onDestroyView () {
268+ // Ensure that all animations are canceled
269+ feedBinding.newItemsLoadedButton?.clearAnimation()
270+
267271 feedBinding.itemsList.adapter = null
268272 _feedBinding = null
269273 super .onDestroyView()
@@ -619,9 +623,9 @@ class FeedFragment : BaseStateFragment<FeedState>() {
619623 }
620624
621625 private fun showNewItemsLoaded () {
622- feedBinding.newItemsLoadedButton .clearAnimation()
623- feedBinding.newItemsLoadedButton
624- .slideUp(
626+ tryGetNewItemsLoadedButton()? .clearAnimation()
627+ tryGetNewItemsLoadedButton()
628+ ? .slideUp(
625629 250L ,
626630 delay = 100 ,
627631 execOnEnd = {
@@ -636,23 +640,32 @@ class FeedFragment : BaseStateFragment<FeedState>() {
636640 }
637641
638642 private fun hideNewItemsLoaded (animate : Boolean , delay : Long = 0) {
639- feedBinding.newItemsLoadedButton .clearAnimation()
643+ tryGetNewItemsLoadedButton()? .clearAnimation()
640644 if (animate) {
641- feedBinding.newItemsLoadedButton .animate(
645+ tryGetNewItemsLoadedButton()? .animate(
642646 false ,
643647 200 ,
644648 delay = delay,
645649 execOnEnd = {
646650 // Make the layout invisible so that the onScroll toTop method
647651 // only does necessary work
648- feedBinding?.newItemsLoadedButton ?.isVisible = false
652+ tryGetNewItemsLoadedButton() ?.isVisible = false
649653 }
650654 )
651655 } else {
652- feedBinding.newItemsLoadedButton .isVisible = false
656+ tryGetNewItemsLoadedButton()? .isVisible = false
653657 }
654658 }
655659
660+ /* *
661+ * The view/button can be disposed/set to null under certain circumstances.
662+ * E.g. when the animation is still in progress but the view got destroyed.
663+ * This method is a helper for such states and can be used in affected code blocks.
664+ */
665+ private fun tryGetNewItemsLoadedButton (): Button ? {
666+ return _feedBinding ?.newItemsLoadedButton
667+ }
668+
656669 // /////////////////////////////////////////////////////////////////////////
657670 // Load Service Handling
658671 // /////////////////////////////////////////////////////////////////////////
0 commit comments