Hi everyone I’m doing horizontal pager infinite l...
# compose
t
Hi everyone I’m doing horizontal pager infinite loop and i see this bug. When i add job
animateScrollToPage
in
LaunchedEffect
, the screen will show 1/2 ui pager when job is working. But when i add job
animateScrollToPage
in
coroutineScope
, it’ll work correct. This is bug
LauncherEffect
in compose? So somebody can explain me the reason?
y
Check if
pagerState.currentPage
is changing before the animation is completed. It would cause the
LaunchedEffect
block to be cancelled (and thus the animation) You can also use a snapshotFlow on currentPage to observe and scroll (removing the key from LaunchedEffect would fix the bug + avoid unnecessary recomposition)
❤️ 1
t
@Yann Badoual you mean i should use snapshotFlow instead of launcherEffect? I’m doing flow your answer but it still happen. Can u give me example code 😂 .
x
The adequate scope to update the pager state should be a CoroutineScope obtained using
rememberCoroutineScope
. The reason is that
LaunchedEffect
is only triggered when you enter the composition phase, but a remembered CoroutineScope could be used to trigger async events at any other point outside of composition. This article by @Jorge Castillo explains it pretty neatly --> https://newsletter.jorgecastillo.dev/p/jetpack-compose-effect-handlers
🙌 1
t
@xoangon Oh very helpful for me. Thank you a lot 😄
❤️ 1
101 Views