总的来说,看看代码,我不明白为什么可以这样做。https://github.com/google/iosched

在应用程序/iosched/ui/MainActivity.kt中,它初始化了NavController和NavHostFragment,但似乎没有特殊待遇。https://github.com/google/iosched/blob/main/mobile/src/main/java/com/google/samples/apps/iosched/ui/MainActivity.kt
val appBarConfiguration = AppBarConfiguration(TOP_LEVEL_DESTINATIONS)
private val TOP_LEVEL_DESTINATIONS = setOf(
R.id.navigation_feed,
R.id.navigation_schedule,
R.id.navigation_map,
R.id.navigation_info,
// R.id.navigation_agenda, comment will not stop saving the statement.
R.id.navigation_codelabs,
R.id.navigation_settings
)It :最简单的片段,BindingAdapter方法总是插入AgendaAdapter(),但是它可以保存init之后的RecyclerView位置。https://github.com/google/iosched/blob/main/mobile/src/main/java/com/google/samples/apps/iosched/ui/agenda/AgendaFragment.kt
为什么它可以保存每个片段的状态?
发布于 2022-04-12 01:51:23
IOSched 依赖于导航2.4.1.根据导航2.4.0发行说明
NavController ..。已展开以支持保存和还原后台堆栈。 作为此更改的一部分,
NavigationUI方法(onNavDestinationSelected()、BottomNavigationView.setupWithNavController()和NavigationView.setupWithNavController())现在会自动保存和恢复弹出目的地的状态,从而支持对多个背叠的支持,而不会发生任何代码更改。当使用带片段的导航时,这是与多个back堆栈集成的推荐方法。
和IOSched setupWithNavController,这意味着每个选项卡将自动保存和恢复其状态。
这包括RecyclerView的状态,它始终支持自动保存和恢复其位置。
https://stackoverflow.com/questions/71835989
复制相似问题