如何保存底部导航片段的状态-使用单个导航图的Android导航组件
在使用Android导航组件JetPack时,如何保存每个底部导航片段的状态 我知道有一种方法可以通过Android团队提供的导航扩展来实现虽然它可以工作,但它需要为每个片段创建多个nav_图,并且没有我想要的后堆栈。而且,使用他们的方法在片段之间切换似乎很慢 如何使用单个导航图保存状态并维护每个后堆栈。 我遵循本教程及其工作原理,但不保存每个片段的状态。片段的每个实例都是在单击底部导航项时创建的。- 活动\u home.xml如何保存底部导航片段的状态-使用单个导航图的Android导航组件,android,xml,kotlin,navigation,android-bottomnav,Android,Xml,Kotlin,Navigation,Android Bottomnav,在使用Android导航组件JetPack时,如何保存每个底部导航片段的状态 我知道有一种方法可以通过Android团队提供的导航扩展来实现虽然它可以工作,但它需要为每个片段创建多个nav_图,并且没有我想要的后堆栈。而且,使用他们的方法在片段之间切换似乎很慢 如何使用单个导航图保存状态并维护每个后堆栈。 我遵循本教程及其工作原理,但不保存每个片段的状态。片段的每个实例都是在单击底部导航项时创建的。- 活动\u home.xml <fragment //I get a warning he
<fragment //I get a warning here, when I change to FragmentContainerView, app crashes//
android:id="@+id/nav_host_fragment_2"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@+id/bottom_navigation"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph_2" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:itemTextColor="@color/white"
app:itemIconSize = "30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:labelVisibilityMode="unlabeled"
app:menu="@menu/bottom_navigation"/>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/feedRandomFragment"
android:icon="@drawable/home_bottom_nav_selector"
android:title="@string/home"
android:menuCategory="secondary"
/>
<item
android:id="@+id/exploreAndSearchFragment"
android:icon="@drawable/explore_bottom_nav_selector"
android:title="@string/global_explore"
android:menuCategory="secondary"
/>
<item
android:id="@+id/uploadChooseFragment"
android:icon="@drawable/ic_upload"
android:title="@string/upload"
android:menuCategory="secondary"
/>
<item
android:id="@+id/allChallengesFragment"
android:icon="@drawable/challenges_bottom_nav_selector"
android:title="@string/challenges"
android:menuCategory="secondary"
/>
<item
android:id="@+id/profileCurrentUserFragment"
android:icon="@drawable/profile_bottom_nav_selector"
android:title="@string/profile"
android:menuCategory="secondary"
/>
根据教程,为了维护backstack,我们必须从一个util类BaseBottomTabFragment扩展所有的底层nav片段,我这样做了,而且效果很好
BaseBottomFragment
if(savedInstanceState==null){
setUpBottomNavigationBarBase()
}
private fun setUpBottomNavigationBarBase(){
binding.bottomNavigation.setupWithNavController(Navigation.findNavController(this,
R.id.nav_host_fragment_2))
binding.bottomNavigation.setOnNavigationItemSelectedListener {item ->
onNavDestinationSelected(item, Navigation.findNavController(this, R.id.nav_host_fragment_2))
}
binding.bottomNavigation.itemIconTintList = null
binding.bottomNavigation.setOnNavigationItemReselectedListener {
//do something
}
}
open class BaseBottomTabFragment : Fragment() {
var isNavigated = false
fun navigateWithAction(action: NavDirections) {
isNavigated = true
findNavController().navigate(action)
}
fun navigate(resId: Int) {
isNavigated = true
findNavController().navigate(resId)
}
override fun onDestroyView() {
super.onDestroyView()
if (!isNavigated)
requireActivity().onBackPressedDispatcher.addCallback(this) {
val navController = findNavController()
if (navController.currentBackStackEntry?.destination?.id != null) {
findNavController().popBackStackAllInstances(
navController.currentBackStackEntry?.destination?.id!!,
true
)
} else
navController.popBackStack()
}
}
private fun NavController.popBackStackAllInstances(destination: Int, inclusive: Boolean): Boolean {
var popped: Boolean
while (true) {
popped = popBackStack(destination, inclusive)
if (!popped) {
break
}
}
return popped
}
}
因此,我所有的底部选项卡片段都是从util类BaseBottomTabFragment扩展而来的,如下所示:
class ExploreAndSearchFragment : BaseBottomTabFragment()
另外,根据教程,为了保持片段的状态并避免重新创建,每个片段都必须有一个唯一的ID,我也这么做了——遗憾的是,这并不能阻止片段重新创建onClick
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".fragments.main.bottomnav.home.view.FeedRandomFragment"
android:fillViewport="true"
android:background="@color/white"
android:id="@+id/homeId">
我为所有片段设置了一个唯一的id,但它不起作用。请帮帮我