https://kotlinlang.org logo
#compose
Title
# compose
y

yschimke

12/09/2021, 10:33 AM
For a component like Accompanist HorizontalPager, the docs state
Pages in both
HorizontalPager
and
VerticalPager
are lazily composed and laid-out as required by the layout. As the user scrolls through pages, any pages which are no longer required are removed from the content.
Does this mean it is safe to use LaunchedEffect as a signal that the current page is just been shown? Or do they get created eagerly when user is contemplating a scroll/starting a swipe? Should pager state change events be used instead?
c

cb

12/09/2021, 10:43 AM
A
LaunchedEffect
reading what state? But yes, just use the
PagerState.currentPage
, it's what it is there for
y

yschimke

12/09/2021, 10:58 AM
Thanks, that's perfect. It's actually going to be a DisposableEffect, needs to set up an event listener when the page goes on screen, and removes it when it leaves, replaced by handler in UI on another page. So sounds like I've introduced a bug using anything triggered on Composition, rather than the currentPage.
a

Albert Chang

12/09/2021, 2:44 PM
Note that `LaunchedEffect`/`DisposableEffect` won't work as you expected in a lazy list. As Chris suggested,
currentPage
is what you should use. See this.
👍🏻 1