diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/pager/BaseReaderFragment.kt b/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/pager/BaseReaderFragment.kt index 57fe76149..f83a2edd2 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/pager/BaseReaderFragment.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/pager/BaseReaderFragment.kt @@ -25,11 +25,26 @@ abstract class BaseReaderFragment : BaseFragment(), ZoomCont readerAdapter = onCreateAdapter() viewModel.content.observe(viewLifecycleOwner) { - if (it.state == null && it.pages.isNotEmpty() && readerAdapter?.hasItems != true) { - onPagesChanged(it.pages, viewModel.getCurrentState()) - } else { - onPagesChanged(it.pages, it.state) + // Determine which state to use for restoring position: + // - content.state: explicitly set state (e.g., after mode switch or chapter change) + // - getCurrentState(): current reading position saved in SavedStateHandle + val currentState = viewModel.getCurrentState() + val pendingState = when { + // If content.state is null and we have pages, use getCurrentState + it.state == null + && it.pages.isNotEmpty() + && readerAdapter?.hasItems != true -> currentState + + // use currentState only if it matches the current pages (to avoid the error message) + readerAdapter?.hasItems != true + && it.state != currentState + && currentState != null + && it.pages.any { page -> page.chapterId == currentState.chapterId } -> currentState + + // Otherwise, use content.state (normal flow, mode switch, chapter change) + else -> it.state } + onPagesChanged(it.pages, pendingState) } }