导航组件的popUpTo不删除按钮 [英] Navigation Component's popUpTo not removing up button

查看:308
本文介绍了导航组件的popUpTo不删除按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用导航体系结构组件,并且具有类似于的设置,用于在导航时弹出堆栈到特定片段:

I'm using the Navigation Architecture Component and I have a setup similar to this one for popping the stack when navigating to a particular fragment:

<action
  android:id="@+id/navigate_to_main_screen"
  app:destination="@id/fragment_main_screen"
  app:popUpTo="@+id/navigation_main"
  app:popUpToInclusive="true"/>

这几乎可以预期.应用程序栏中的系统后退按钮和向上图标都不会导航到上一个片段.系统后退按钮退出应用程序.

This works almost as expected. Both the system back button and the up icon in the app bar don't navigate to the previous fragment. The system back button exits the app.

但是,应用程序栏中的向上按钮仍然存在,单击该按钮不会执行任何预期的操作.我究竟做错了什么?为什么还在这里?

However, the up button in the app bar is still there, clicking it doesn't do anything as expected. What am I doing wrong? Why is this still here?

在主要活动中我已经拥有

In the main activity I already have

AppBarConfiguration config =
    new AppBarConfiguration.Builder(navController.getGraph()).build();
NavigationUI.setupActionBarWithNavController(this, navController, config);

@Override
public boolean onSupportNavigateUp() {
  return navController.navigateUp() || super.onSupportNavigateUp();
}

根据文档.

我正在使用的库版本:

implementation 'android.arch.navigation:navigation-fragment:1.0.0-alpha09'
implementation 'android.arch.navigation:navigation-ui:1.0.0-alpha09'

推荐答案

如果要自定义哪些目标被视为顶级目标,则可以将一组目标ID传递给构造函数,如下所示.

If you want to customize which destinations are considered top-level destinations, you can instead pass a set of destination IDs to the constructor, as shown below.

要解决您的问题,请替换

To solve your problem, replace

AppBarConfiguration config =
    new AppBarConfiguration.Builder(navController.getGraph()).build();

使用

AppBarConfiguration config =
        new AppBarConfiguration.Builder(R.id.navigation_main, R.id.fragment_main_screen).build();

此处有更多详细信息: AppBarConfiguration

More details here: AppBarConfiguration

这篇关于导航组件的popUpTo不删除按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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