我看到了很多关于 ViewPager 的问题,但现在它已经上线了ViewPager2.
我得到了一个java.lang.RuntimeException: android.os.TransactionTooLargeException: data parcel
与ViewPager2。我见过那个TooLargeTool https://github.com/guardian/toolargetool可以帮助确定这个 TooLargeException 可能来自哪里。我的捆绑包是bestof_date
并且相当低(0.1KB),但是android:support:fragments
只是不断增加
所有这些都来自使用 ViewPager + Recyclerview 标准实现FragmentStateAdapter
因为我可以有 150 多个标签
2021-05-14 17:08:16.373 com.my.application D/TooLargeTool: MainActivity.onSaveInstanceState wrote: Bundle167873131 contains 10 keys and measures 43.7 KB when serialized as a Parcel
* com.google.app_measurement.screen_service = 0.2 KB
* KEY_COMPONENT_ACTIVITY_RANDOM_OBJECT = 0.2 KB
* android:viewHierarchyState = 1.9 KB
* KEY_COMPONENT_ACTIVITY_REGISTERED_KEYS = 9.8 KB
* androidx.lifecycle.BundlableSavedStateRegistry.key = 30.3 KB
* KEY_COMPONENT_ACTIVITY_REGISTERED_RCS = 0.6 KB
* KEY_COMPONENT_ACTIVITY_PENDING_RESULT = 0.1 KB
* KEY_COMPONENT_ACTIVITY_LAUNCHED_KEYS = 0.1 KB
* android:lastAutofillId = 0.1 KB
* android:fragments = 0.4 KB
2021-05-14 17:08:16.482 com.my.application D/TooLargeTool: BestofVideosFragment.onSaveInstanceState wrote: Bundle168360416 contains 3 keys and measures 1.2 KB when serialized as a Parcel
* androidx.lifecycle.BundlableSavedStateRegistry.key = 0.1 KB
* android:view_registry_state = 0.2 KB
* android:view_state = 0.9 KB
* fragment arguments = Bundle87348377 contains 1 keys and measures 0.1 KB when serialized as a Parcel
* bestof_date = 0.1 KB
2021-05-14 17:08:17.003 com.my.application D/TooLargeTool: BestofFragment.onSaveInstanceState wrote: Bundle113147201 contains 4 keys and measures 24.0 KB when serialized as a Parcel
* android:support:fragments = 22.5 KB
* androidx.lifecycle.BundlableSavedStateRegistry.key = 0.1 KB
* android:view_registry_state = 0.2 KB
* android:view_state = 1.2 KB
* fragment arguments = Bundle176902886 contains 0 keys and measures 0.0 KB when serialized as a Parcel
2021-05-14 17:08:17.075 com.my.application D/TooLargeTool: NavHostFragment.onSaveInstanceState wrote: Bundle75010087 contains 7 keys and measures 29.1 KB when serialized as a Parcel
* android:support:fragments = 26.9 KB
* androidx.lifecycle.BundlableSavedStateRegistry.key = 0.1 KB
* android-support-nav:fragment:defaultHost = 0.1 KB
* android:view_registry_state = 0.2 KB
* android-support-nav:fragment:graphId = 0.1 KB
* android-support-nav:fragment:navControllerState = 1.5 KB
* android:view_state = 0.1 KB
更新1:
经过更多搜索后,我发现这是因为FragmentStateAdapter
正在保存堆积的视图状态,直到达到最大容量并崩溃,有没有办法清除它?
事实证明,我强制 navController 保存用户访问的每个屏幕,因此创建了一个大的后台堆栈。
bottomNavigation?.setOnItemSelectedListener {
navController.navigate(it.itemId)
true
}
我使用这个片段是因为我在底部栏中的一个项目中有一个 BottomSheetDialog,当单击它时,导航会将我送回 startDestination。
我改为https://developer.android.com/reference/androidx/navigation/ui/NavigationUI#onnavdestinationselected https://developer.android.com/reference/androidx/navigation/ui/NavigationUI#onnavdestinationselected
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="@string/title_home"
android:id="@id/homeFragment"
android:icon="@drawable/ic_home"/>
<item android:title="@string/title_more"
android:id="@id/plusFragment"
android:icon="@drawable/ic_plus"
android:menuCategory="secondary" />
</menu>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)