android - Android Jetpack Navigation、BottomNavigationView 与 Youtube 或 Instagram 类似正确的后退导航(片段后退堆栈)?
问题描述
Android Jetpack Navigation,BottomNavigationView 在后退按钮单击时具有自动片段返回堆栈?
我想要的是,在用户一个接一个地选择多个选项卡后,用户单击后退按钮应用程序必须重定向到他/她打开的最后一页。
我使用 Android ViewPager 实现了相同的目的,方法是将当前选定的项目保存在 ArrayList 中。Android Jetpack Navigation 发布后是否有自动回栈?我想使用导航图来实现它
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".main.MainActivity">
<fragment
android:id="@+id/my_nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@+id/navigation"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/nav_graph" />
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation" />
</android.support.constraint.ConstraintLayout>
导航.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_home"
android:icon="@drawable/ic_home"
android:title="@string/title_home" />
<item
android:id="@+id/navigation_people"
android:icon="@drawable/ic_group"
android:title="@string/title_people" />
<item
android:id="@+id/navigation_organization"
android:icon="@drawable/ic_organization"
android:title="@string/title_organization" />
<item
android:id="@+id/navigation_business"
android:icon="@drawable/ic_business"
android:title="@string/title_business" />
<item
android:id="@+id/navigation_tasks"
android:icon="@drawable/ic_dashboard"
android:title="@string/title_tasks" />
</menu>
还添加了
bottomNavigation.setupWithNavController(Navigation.findNavController(this, R.id.my_nav_host_fragment))
我得到了一个答案Levi Moreira
,如下
navigation.setOnNavigationItemSelectedListener {item ->
onNavDestinationSelected(item, Navigation.findNavController(this, R.id.my_nav_host_fragment))
}
但是这样做只会发生最后打开的片段的实例再次创建。
为 BottomNavigationView 提供正确的后退导航
解决方案
您真的不需要使用ViewPager
新BottomNavigation
的导航架构组件。我一直在使用恰好使用这两者的示例应用程序,请参见此处。
基本概念是这样的,您拥有将托管的主要活动,BottomNavigationView
即导航图的导航主机,这就是它的 xml 的样子:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".main.MainActivity">
<fragment
android:id="@+id/my_nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@+id/navigation"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/nav_graph" />
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation" />
</android.support.constraint.ConstraintLayout>
导航菜单(选项卡菜单)BottomNavigationView
如下所示:
导航.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_home"
android:icon="@drawable/ic_home"
android:title="@string/title_home" />
<item
android:id="@+id/navigation_people"
android:icon="@drawable/ic_group"
android:title="@string/title_people" />
<item
android:id="@+id/navigation_organization"
android:icon="@drawable/ic_organization"
android:title="@string/title_organization" />
<item
android:id="@+id/navigation_business"
android:icon="@drawable/ic_business"
android:title="@string/title_business" />
<item
android:id="@+id/navigation_tasks"
android:icon="@drawable/ic_dashboard"
android:title="@string/title_tasks" />
</menu>
所有这些只是BottomNavigationView
设置。现在要使其与 Navigation Arch 组件一起使用,您需要进入导航图编辑器,添加所有片段目的地(在我的情况下,我有 5 个,每个选项卡一个)并将目的地的 id 设置为相同名称为navigation.xml
文件中的名称:
这将告诉 android 在选项卡和片段之间建立链接,现在每次用户单击“主页”选项卡时,android 都会负责加载正确的片段。还需要将一段 kotlin 代码添加到您的 NavHost(主要活动)中,以将其与BottomNavigationView
:
您需要在 onCreate 中添加:
bottomNavigation.setupWithNavController(Navigation.findNavController(this, R.id.my_nav_host_fragment))
这告诉 android 在 Navigation 架构组件和 BottomNavigationView 之间进行连接。在文档中查看更多信息。
要获得与使用 youtube 时相同的行为,只需添加以下内容:
navigation.setOnNavigationItemSelectedListener {item ->
onNavDestinationSelected(item, Navigation.findNavController(this, R.id.my_nav_host_fragment))
}
这将使目的地进入后台堆栈,因此当您点击后退按钮时,将弹出最后访问的目的地。
推荐阅读
- python - 实现多变量和多维数据聚类分析的k-means算法
- database - 询问数据库管理员密码以便在 Odoo 12 中执行某些操作
- javascript - 以时间间隔显示和隐藏 for 循环中的元素
- r - 检查数据框中两个变量的一致性(R)
- sql - 如何从查询中计算百分比?
- mysql - MySql 似乎改变了它处理字符串变量的方式
- posix - POSIX 和 RISC/CISC 有什么关系
- android - 如何在android中关闭材料设计EditText的默认功能?
- reactjs - 用 jest 和酵素测试 antd 验证器
- ios - 如何在 Realm 中使用 @Count 过滤器计数?