How to fix TransactionTooLargeException when using viewPager2 with a lot of fragments

97 Views Asked by At

I have a case where I want to use viewPager2 with dynamic number of fragments, sometimes they are 1000+ fragments, each fragment contains a "long" argument. The issue is when i navigate to a large number of fragments (50+) and then put the app in background, I will get TransactionTooLargeException. From the logs it looks that the parcel size is too large because of "android:support:fragments".

This is the FragmentStateAdapter:

class MyAdapter(fragment: Fragment, private val ids: List<Long>) :
    FragmentStateAdapter(fragment) {

    override fun getItemCount(): Int = ids.size

    override fun createFragment(position: Int): Fragment {
        return MyFragment.newInstance(ids[position])
    }
}

and this is the logs:

E !!! FAILED BINDER TRANSACTION !!! (parcel size = 1617044) W Bundle stats: W androidx.lifecycle.BundlableSavedStateRegistry.key [size=1615712] W android:support:activity-result [size=6192] W KEY_COMPONENT_ACTIVITY_REGISTERED_KEYS [size=5524] W android:support:fragments [size=1608532] W fragment_153267f4-13db-4e3e-aadc-397b3613c3b0 [size=1608036] W Ignoring header If-None-Match because its value was null. W childFragmentManager [size=1604160] W fragment_5e76ed4f-86c4-4abd-b180-a266b7ecb7ec [size=3432] W viewState [size=1480] W state [size=2492] W fragment_e8dab6b8-c161-4b3f-bfc2-5e8144a830b5 [size=1589116] W viewState [size=1562464] W 0x7f0a075f [size=1561320] W childFragmentManager [size=25000] W fragment_d802e2b3-6eab-4b0c-ab8f-58f71bd734c4 [size=5968] W viewState [size=4432] W fragment_c9746424-d404-43a8-9886-5c7107e952a9 [size=5968] W viewState [size=4432] W fragment_d34a0450-4f4d-4c7f-9291-a4bdb9a127c3 [size=5908] W viewState [size=4372] W fragment_bfc173a7-454f-4313-b309-68256713769e [size=5968] W viewState [size=4432] W fragment_a78c4302-7fb2-4e0c-9c6f-8eab1c57afdc [size=4908] W viewState [size=1572] W childFragmentManager [size=2536] W fragment_02f2a322-316e-4560-8ded-ceeefdfd9222 [size=2116] W fragment_051e13d4-04fc-4571-a076-48795ec8cbef [size=3796] W viewState [size=2040] W registryState [size=3184] W androidx.lifecycle.BundlableSavedStateRegistry.key [size=3060] W android-support-nav:fragment:navControllerState [size=2640] W android-support-nav:controller:backStack [size=2536] W PersistableBundle stats: W [null]

I have tried to comment out onSavedInstanceState method, but it didn't work.

0

There are 0 best solutions below