From c4834a44d1996945980e628a0e08a722a9fdff6d Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Thu, 5 May 2022 15:28:17 +0100 Subject: [PATCH] aligning the carousel listener removal with the viewLifecycleOwner - fixes crash where the scheduled callbacks can attempt to trigger after the view has been destroyed --- .../ftueauth/FtueAuthSplashCarouselFragment.kt | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/vector/src/main/java/im/vector/app/features/onboarding/ftueauth/FtueAuthSplashCarouselFragment.kt b/vector/src/main/java/im/vector/app/features/onboarding/ftueauth/FtueAuthSplashCarouselFragment.kt index 05295c5b8d..30416bde9e 100644 --- a/vector/src/main/java/im/vector/app/features/onboarding/ftueauth/FtueAuthSplashCarouselFragment.kt +++ b/vector/src/main/java/im/vector/app/features/onboarding/ftueauth/FtueAuthSplashCarouselFragment.kt @@ -22,6 +22,8 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.core.view.isVisible +import androidx.lifecycle.DefaultLifecycleObserver +import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.lifecycleScope import androidx.viewpager2.widget.ViewPager2 import com.airbnb.mvrx.withState @@ -90,7 +92,7 @@ class FtueAuthSplashCarouselFragment @Inject constructor( private fun ViewPager2.registerAutomaticUntilInteractionTransitions() { var scheduledTransition: Job? = null - registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() { + val pageChangingCallback = object : ViewPager2.OnPageChangeCallback() { private var hasUserManuallyInteractedWithCarousel: Boolean = false override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) { @@ -104,6 +106,15 @@ class FtueAuthSplashCarouselFragment @Inject constructor( scheduledTransition = scheduleCarouselTransition() } } + } + viewLifecycleOwner.lifecycle.addObserver(object : DefaultLifecycleObserver { + override fun onCreate(owner: LifecycleOwner) { + registerOnPageChangeCallback(pageChangingCallback) + } + + override fun onDestroy(owner: LifecycleOwner) { + unregisterOnPageChangeCallback(pageChangingCallback) + } }) }