Skip to content

Commit

Permalink
revert fix ViewPager2 crash to avoid TransactionTooLargeException
Browse files Browse the repository at this point in the history
  • Loading branch information
Razeeman committed Feb 1, 2025
1 parent fd1f2c4 commit 6bb7519
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ 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 @@ -83,6 +83,11 @@ class SafeFragmentStateAdapter(

override fun restoreState(savedState: Parcelable) {
try {
// Restoring is crashing with "Expected the adapter to be 'fresh' while restoring state".
// Don't need it anyway, because binding is saved in BaseFragment.
// Crash can be avoided with setSaveEnabled,
// which would cause skipping of saveState / restoreState,
// but it will cause fragments to save themselves which would cause TransactionTooLargeException.
adapter.restoreState(savedState)
} catch (e: Exception) {
Timber.e(e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ 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,6 @@ class RecordsContainerFragment :

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

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

0 comments on commit 6bb7519

Please sign in to comment.