Combining stateFlows is getting triggered even though value is not changed in kotlin android

149 views Asked by At
private val _searchQuery = MutableStateFlow("")
val searchQuery = _searchQuery.asStateFlow()

private val _activeSort = MutableStateFlow(HomeScreenSortOptions.MOST_RELEVANT)
val activeSort = _activeSort.asStateFlow()

@OptIn(FlowPreview::class, ExperimentalCoroutinesApi::class)
val homeScreenItemsPager =
    combine(
        searchQuery.debounce(SEARCH_QUERY_DEBOUNCE_TIME).distinctUntilChanged(),
        activeSort
    ) { query, sort ->
        query to sort
    }.flatMapLatest {
        Pager(
            config = PagingConfig(
                pageSize = HOME_SCREEN_PAGE_SIZE,
                prefetchDistance = HOME_SCREEN_PAGE_SIZE
            )
        ) {
            HomeScreenItemSource(
                query = searchQuery.value,
                sort = activeSort.value.value,
                homeRepository = homeRepository
            )
        }.flow.cachedIn(scope = viewModelScope)
    }

Here I use pagination to show the list. But when I go to detail page of any item & come back to list page again, then combine stateFlow gets re-triggered even though none of the values have changed & entire list refreshes again.
And I even loose the last scrolled position as well. How can I fix this

0

There are 0 answers