Android Jetpack Navigation、BottomNavigationView 与 Youtube 或 Instagram 类似正确的后退导航( fragment 后退堆栈)?

标签 android android-architecture-components bottomnavigationview android-navigation android-architecture-navigation

Android Jetpack 导航,BottomNavigationView 在后退按钮单击时具有自动 fragment 返回堆栈?

我想要的是,在用户一个接一个地选择多个标签后,用户点击后退按钮应用程序必须重定向到他/她打开的最后一页。

我使用 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>

navigation.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))

        }

但是这样做只会发生上次打开的 fragment 的实例再次创建。

为 BottomNavigationView 提供适当的后退导航

最佳答案

您实际上并不需要 ViewPager 来使用 BottomNavigation 和新的导航架构组件。我一直在使用恰好使用这两个的示例应用程序,请参阅 here .

基本概念是这样的,您拥有将承载 BottomNavigationView 的主要 Activity ,它是导航图的导航主机,这就是它的 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 的导航菜单(选项卡菜单)如下所示:

navigation.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 组件一起使用,您需要进入导航图编辑器,添加所有 fragment 目的地(在我的情况下,我有 5 个,每个选项卡一个)并将目的地的 id 设置为相同名称为 navigation.xml 文件中的名称:

enter image description here

这将告诉 android 在选项卡和 fragment 之间建立链接,现在每次用户单击“主页”选项卡时,android 都会负责加载正确的 fragment 。 还有一段 kotlin 代码需要添加到您的 NavHost(主要 Activity )中,以将其与 BottomNavigationView 连接起来:

您需要在 onCreate 中添加:

bottomNavigation.setupWithNavController(Navigation.findNavController(this, R.id.my_nav_host_fragment))

这告诉 android 在 Navigation 架构组件和 BottomNavigationView 之间进行连接。在 docs 中查看更多信息.

要获得与使用 youtube 时相同的行为,只需添加以下内容:

navigation.setOnNavigationItemSelectedListener {item ->

            onNavDestinationSelected(item, Navigation.findNavController(this, R.id.my_nav_host_fragment))

        }

这将使目的地进入后台堆栈,因此当您点击后退按钮时,将弹出最后访问的目的地。

关于Android Jetpack Navigation、BottomNavigationView 与 Youtube 或 Instagram 类似正确的后退导航( fragment 后退堆栈)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50577356/

相关文章:

Android fragment 布局完成膨胀

java - 在 Android 应用中仅显示截止日期为今天的待办事项

带有即时应用程序的 android 架构组件

java - 当点击从 WorkManager 发送的通知时,使用导航组件启动特定 fragment

java.lang.IllegalArgumentException : This source was already added with the different observer 异常

java - 使用android导航组件时,有没有办法防止在bottomNavigation中重新创建 fragment

java - 使用自定义图标时增加导航底部 View android中选定图标的高度?

android - Android模拟器的键盘快捷键?

android - 使 BottomNavigationView 停留在其他 View 下

java - 如何从 AsyncTask 返回 boolean 值?