aligning the carousel listener removal with the viewLifecycleOwner

- fixes crash where the scheduled callbacks can attempt to trigger after the view has been destroyed
This commit is contained in:
Adam Brown 2022-05-05 15:28:17 +01:00
parent a4b5d1819d
commit c4834a44d1
1 changed files with 12 additions and 1 deletions

View File

@ -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)
}
})
}