在使用Android导航组件JetPack时如何保存每个底部导航片段的状态。
我知道有一种方法可以使用Android团队提供的导航扩展-Navigation Extension。 -虽然有效,但它要求您为每个片段创建多个nav_graph,并且也没有我想要的反向堆栈。此外,使用他们的方法,在片段之间切换似乎很慢。
如何使用单个nav_graph保存状态并维护每个后退堆栈。 我正在关注本教程及其工作,但没有保存每个片段的状态。片段的每个实例都是在底部导航项的“单击”上创建的。 -Bottom Nav Tutorial Like Instagram And Youtube
activity_home.xml
<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"/>
菜单/bottom_navigation.xml
<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"
/>
HomeActivity.kt
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
}
}
根据该教程,要维护Backstack,我们必须从我做的很好的util类BaseBottomTabFragment扩展所有底部的nav片段。
BaseBottomFragment
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,但是没有用。请帮帮我!