Skip to content

Commit

Permalink
aligning the carousel listener removal with the viewLifecycleOwner
Browse files Browse the repository at this point in the history
- fixes crash where the scheduled callbacks can attempt to trigger after the view has been destroyed
  • Loading branch information
ouchadam committed May 5, 2022
1 parent 0c556a2 commit 6ae54a5
Showing 1 changed file with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
}
})
}

Expand Down

0 comments on commit 6ae54a5

Please sign in to comment.