【问题标题】:How to save state of Bottom Navigation Fragment - Android Navigation Component With a Single Nav Graph如何保存底部导航片段的状态 - 具有单个导航图的 Android 导航组件
【发布时间】:2021-02-21 08:41:30
【问题描述】:

在使用 Android Navigation Component 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"/>

menu/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 扩展所有底部导航片段。

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,但它不起作用。请帮帮我!

【问题讨论】:

  • 你解决了这个问题吗?如果请分享概念或代码。谢谢!

标签: android xml kotlin navigation android-bottomnav


【解决方案1】:

由于我遇到了类似的问题,我从他们的GitHub page 复制了导航高级示例并开始进行故障排除。

我的目标是在拥有类似 Instagram 的底部导航栏的同时使用多个后退堆栈。所以我想将 MAD Skills 的教程Navigation: Multiple back stacks 与 Furkan Aşkın 的Instagram-like back stack guide

当我实现了这两个组件时,项目编译完美,但多个后退堆栈似乎无法正常工作,因为底部选项卡的状态没有保存。我仔细检查了版本是否为 2.4.0-alpha01 或更高版本。

问题出在菜单项中定义的二级menuCategory:

android:menuCategory="secondary"

这可能会覆盖多个返回堆栈的预期行为,并且在删除行后,多个返回堆栈可以正常工作。此外,在不同的底部导航顶级片段中导航时,删除了多余的后退堆栈。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-19
    • 2019-10-05
    • 2021-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多