Skip to content

Commit

Permalink
fix ViewPager2 crash
Browse files Browse the repository at this point in the history
  • Loading branch information
Razeeman committed Jan 11, 2025
1 parent a1de6f4 commit 0349b92
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ abstract class BaseFragment<T : ViewBinding> : Fragment(), Throttler {
// This also somehow fixes memory leaks occurring on navigation
// from main to some edit screen and back.
// If this ever changes - need to also fix these memory leaks.
// Also ViewPagers has isSaveEnabled set to false, to avoid crash
// "Expected the adapter to be 'fresh' while restoring state".
return _binding?.root?.also {
initialized = true
} ?: run {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class MainFragment : BaseFragment<Binding>() {
}

private fun setupPager() = with(binding) {
mainPager.isSaveEnabled = false // See BaseFragment.
mainPager.adapter = SafeFragmentStateAdapter(
MainContentAdapter(
fragment = this@MainFragment,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ class RecordsContainerFragment :

override fun initUi(): Unit = with(binding) {
pagerRecordsContainer.apply {
adapter = SafeFragmentStateAdapter(RecordsContainerAdapter(this@RecordsContainerFragment))
isSaveEnabled = false // See BaseFragment.
adapter = SafeFragmentStateAdapter(
RecordsContainerAdapter(this@RecordsContainerFragment),
)
offscreenPageLimit = 1
isUserInputEnabled = false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ class StatisticsContainerFragment :

override fun initUi(): Unit = with(binding) {
pagerStatisticsContainer.apply {
adapter = SafeFragmentStateAdapter(StatisticsContainerAdapter(this@StatisticsContainerFragment))
isSaveEnabled = false // See BaseFragment.
adapter = SafeFragmentStateAdapter(
StatisticsContainerAdapter(this@StatisticsContainerFragment),
)
offscreenPageLimit = 1
isUserInputEnabled = false
}
Expand Down

0 comments on commit 0349b92

Please sign in to comment.