我正在尝试使用新的导航组件.我使用带有navController的BottomNavigationVIEw:NavigationUI.setupWithNavController(bottomNavigation,navController)
但是当我切换片段时,即使它们以前被使用过,它们也会每次都被破坏/创建.
有没有办法让我们的主要片段链接到我们的BottomNavigationVIEw?
解决方法:
尝试这个.
航海家
创建自定义导航器.
@Navigator.name("custom_fragment") // Use as custom tag at navigation.xmlclass CustomNavigator( private val context: Context, private val manager: FragmentManager, private val containerID: Int) : FragmentNavigator(context, manager, containerID) { overrIDe fun navigate(destination: Destination, args: Bundle?, navOptions: NavOptions?) { val tag = destination.ID.toString() val transaction = manager.beginTransaction() val currentFragment = manager.primaryNavigationFragment if (currentFragment != null) { transaction.detach(currentFragment) } var fragment = manager.findFragmentByTag(tag) if (fragment == null) { fragment = destination.createFragment(args) transaction.add(containerID, fragment, tag) } else { transaction.attach(fragment) } transaction.setPrimaryNavigationFragment(fragment) transaction.setReorderingallowed(true) transaction.commit() dispatchOnNavigatorNavigated(destination.ID, BACK_STACK_DESTINATION_ADDED) }}
NavHostFragment
创建自定义NavHostFragment.
class CustomNavHostFragment: NavHostFragment() { overrIDe fun createFragmentNavigator(): Navigator<out FragmentNavigator.Destination> { return CustomNavigator(requireContext(), childFragmentManager, ID) }}
navigation.xml
使用自定义标记而不是片段标记.
<navigation xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:app="http://schemas.androID.com/apk/res-auto" androID:ID="@+ID/navigation" app:startDestination="@ID/navigation_first"> <custom_fragment androID:ID="@+ID/navigation_first" androID:name="com.example.sample.FirstFragment" androID:label="FirstFragment" /> <custom_fragment androID:ID="@+ID/navigation_second" androID:name="com.example.sample.SecondFragment" androID:label="SecondFragment" /></navigation>
活动布局
使用CustomNavHostFragment而不是NavHostFragment.
<androIDx.constraintlayout.Widget.ConstraintLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:app="http://schemas.androID.com/apk/res-auto" androID:ID="@+ID/container" androID:layout_wIDth="match_parent" androID:layout_height="match_parent"> <fragment androID:ID="@+ID/nav_host_fragment" androID:name="com.example.sample.CustomNavHostFragment" androID:layout_wIDth="0dp" androID:layout_height="0dp" app:layout_constraintBottom_totopOf="@+ID/bottom_navigation" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constrainttop_totopOf="parent" app:navGraph="@navigation/navigation" /> <com.Google.androID.material.bottomnavigation.BottomNavigationVIEw androID:ID="@+ID/bottom_navigation" androID:layout_wIDth="0dp" androID:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:menu="@menu/navigation" /></androIDx.constraintlayout.Widget.ConstraintLayout>
更新
我创建了示例项目. link
我不创建自定义NavHostFragment.我使用navController.navigatorProvIDer = navigator.
总结以上是内存溢出为你收集整理的android – 当使用BottomNavigationView和新的NavController时,有没有办法让片段保持活着?全部内容,希望文章能够帮你解决android – 当使用BottomNavigationView和新的NavController时,有没有办法让片段保持活着?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)