I’m building a Jetpack Compose application where I need to implement a pull-to-refresh functionality in a LazyColumn. However, I noticed that the state disappears when I navigate to another screen and come back after scrolling lazycolumn and it is stuck on top still refreshing task is going on. But when i am on the screen without navigating and scrolling lazycolumn it is still refreshing that is fine. But after navigate to another screen come back scrolling lazycolumn indicator is stuck on top not in threshold while task is not finished up.
class MyViewModel : ViewModel() {
var itemCount = mutableStateOf(0)
var isRefreshing = mutableStateOf(false)
fun refresh {
isRefreshing.value = true
itemCount.value=0
viewModelScope.launch {
for (i in itemCount.value..15) {
delay(2000)
itemCount.value += 1
}
isRefreshing.value = false
}
}
}
@Composable
fun MyScreen(viewModel: MyViewModel = viewModel()) {
val onRefresh: () -> Unit = {
viewModel.refresh()
}
val itemss by viewModel.itemCount
val isRefreshing by viewModel.isRefreshing
PullToRefreshBox(
state = state,
isRefreshing = isRefreshing,
onRefresh = onRefresh,
) {
LazyColumn(Modifier.fillMaxSize()) {
items(itemss) { ListItem({ Text(text = "Item ${it}") }) }
}
}
}