如何使用单个导航图保存底部导航片段的状态-Android导航组件

如何解决如何使用单个导航图保存底部导航片段的状态-Android导航组件

在使用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,但是没有用。请帮帮我!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?