在导航图的第一个屏幕上的工具栏上显示后退按钮 [英] Display back button on toolbar on first screen of navigation graph

本文介绍了在导航图的第一个屏幕上的工具栏上显示后退按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试androidx导航组件,并使用工具栏和容器设置了我的活动。我是在应用程序的间歇屏幕之一中执行此操作的,该屏幕具有许多内部导航/步骤,我认为我可以尝试使用导航拱门组件。

I'm trying out androidx navigation component and have setup my activity with a toolbar a container. I'm doing this in one of the intermittent screens of my app which has a lot of internal navigation/steps and I thought I could navigation arch component a try with this.

由于这是一个间歇性屏幕,因此我想从第一个屏幕本身开始在工具栏上显示一个后退按钮。

Since this is an intermittent screen, I want to display a back button on the toolbar from the first screen itself.

我已经在主机活动的onCreate()方法中使用以下代码设置了工具栏,

I've already setup the toolbar with the below code in the onCreate() method of my host activity,

 setSupportActionBar(toolbar);
 if (getSupportActionBar() != null) {
   getSupportActionBar().setDisplayHomeAsUpEnabled(true);
   getSupportActionBar().setDisplayShowHomeEnabled(true);
 }
NavigationUI.setupActionBarWithNavController(this, navHostFragment.getNavController());

我可以在图形的第二个屏幕上看到向后按钮/向后箭头,但在第一个屏幕上却看不到

I can see the back button/back arrow on the second screen of my graph but not on the first screen.

<?xml version="1.0" encoding="utf-8"?>
<navigation 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/navigation"
    app:startDestination="@id/listing">

    <fragment
        android:id="@+id/listing"
        android:name=".ui.ItemsListingFragment"
        android:label="Items"
        tools:layout="@layout/items_listing" >
        <action
            android:id="@+id/listToQuantity"
            app:destination="@id/quantity"
           />
        <action
            android:id="@+id/listToReason"
            app:destination="@id/reason"
            />
    </fragment>
    <fragment
        android:id="@+id/quantity"
        android:name=".ui.ItemQuanitySelectionFragment"
        android:label="Items"
        tools:layout="@layout/fragment_item_quanity_selection" >
        <action
            android:id="@+id/quantityToReason"
            app:destination="@id/reason"
            />
    </fragment>
    <fragment
        android:id="@+id/reason"
        android:name=".ui.ItemReasonFragment"
        android:label="Items"
        tools:layout="@layout/fragment_item_reason">
    </fragment>
</navigation>

从第一步开始,要在工具栏上添加后退按钮,我还需要进行哪些更改?

What more changes do I need to make to add the back button on the toolbar right from the first step.

推荐答案

如前所述,导航组件在除顶级目的地之外的所有目的地均显示后退按钮。它使用 AppBarConfiguration 确定哪些目的地被视为顶级。

As you noted, navigation component show back button on all destinations except top-level. It uses an AppBarConfiguration to determine which destinations are considered 'top-level'.

所以您需要:

1)创建没有顶级目标的AppBarConfiguration

2)调用3个参数 setupActionBarWithNavController

AppBarConfiguration abc = new AppBarConfiguration.Builder().build();
NavigationUI.setupActionBarWithNavController(this, navHostFragment.getNavController(), abc)

自定义顶级目标上的按钮行为,您可以在应用栏配置构建器中设置后备 OnNavigateUpListener

To customise up button behavior on 'top-level' destination you can set fallback OnNavigateUpListener in your appbar configuration builder:

new AppBarConfiguration.Builder().setFallbackOnNavigateUpListener(listener).build()

更多信息来源:

单击向上按钮调用 NavigationUI.navigateUp(NavController,AppBarConfiguration),依次调用 NavController.navigateUp()尝试弹出后退堆栈。由于它无法弹出堆栈,因此它什么也不做,并返回false。在这种情况下,您的后备监听器将被调用。

More info from sources:
click on up button calles NavigationUI.navigateUp(NavController, AppBarConfiguration) which in turn calles NavController.navigateUp() which tries to pop the backstack. As it cannot pop back stack, it just does nothing and returns false. Your fallback listener will be called in that case.

这篇关于在导航图的第一个屏幕上的工具栏上显示后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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