Android Jetpack Navigation、BottomNavigationView 与 Youtube 或 Instagram 类似正确的返回导航(片段返回堆栈)? [英] Android Jetpack Navigation, BottomNavigationView with Youtube or Instagram like proper back navigation (fragment back stack)?

本文介绍了Android Jetpack Navigation、BottomNavigationView 与 Youtube 或 Instagram 类似正确的返回导航(片段返回堆栈)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android Jetpack Navigation、BottomNavigationView 在返回按钮点击时具有自动片段返回堆栈?

Android Jetpack Navigation, BottomNavigationView with auto fragment back stack on back button click?

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

What I wanted, after choosing multiple tabs one after another by user and user click on back button app must redirect to the last page he/she opened.

我使用 Android ViewPager 实现了相同的效果,方法是将当前选定的项目保存在 ArrayList 中.Android Jetpack Navigation 发布后是否有任何自动返回堆栈?我想使用导航图来实现它

I achieved the same using Android ViewPager, by saving the currently selected item in an ArrayList. Is there any auto back stack after Android Jetpack Navigation Release? I want to achieve it using navigation graph

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 那里得到了一个答案,如下

I got one answer from Levi Moreira, as follows

navigation.setOnNavigationItemSelectedListener {item ->

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

        }

但是这样做只会发生最后一次打开的片段的实例再次创建.

But by doing this only happening is that last opened fragment's instance creating again.

为 BottomNavigationView 提供适当的后退导航

Providing proper Back Navigation for BottomNavigationView

推荐答案

随着2.4.0版本的导航包,终于正式支持了!

With the version 2.4.0 of the navigation package, it is finally officially supported!

https://developer.android.com/jetpack/androidx/releases/navigation#version_240_2

不仅如此:将导航库上传到此版本后,此功能是默认行为.作为旁注,现在这个默认行为包括在它们之间导航时不会重新创建片段,这似乎是非常需要的东西.

Not only that: after uploading the navigation library to this version, this feature is the default behaviour. And as a side note, now this default behaviour includes that fragments are not recreated when navigating among them, that seemed to be something quite requested.

这篇关于Android Jetpack Navigation、BottomNavigationView 与 Youtube 或 Instagram 类似正确的返回导航(片段返回堆栈)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆