我见过很多关于ViewPager的问题,但现在是在ViewPager2上。
我得到了java.lang.RuntimeException: android.os.TransactionTooLargeException: data parcel
和ViewPager2。我已经看到TooLargeTool
可以帮助确定这个TooLargeException从何而来。我的捆绑包是bestof_date
,相当低(0.1KB),但是android:support:fragments
却一直在增加
所有这些都来自使用ViewPager + 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
保存了堆积的视图状态,直到我们达到最大容量和崩溃为止,有什么方法可以清除这个问题吗?
发布于 2021-08-03 08:53:15
结果,我强迫navController保存用户访问的每个屏幕,因此创建了一个大的后台。
bottomNavigation?.setOnItemSelectedListener {
navController.navigate(it.itemId)
true
}
我之所以使用这个片段,是因为我的一个项目在底部栏中有一个bottomSheetDialog,当单击它时,导航会将我送回startDestination。
我换了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>
https://stackoverflow.com/questions/67540003
复制相似问题