我正在使用导航架构组件并且我有一个类似于 this one 的设置用于在导航到特定 fragment 时弹出堆栈:
<action
android:id="@+id/navigate_to_main_screen"
app:destination="@id/fragment_main_screen"
app:popUpTo="@+id/navigation_main"
app:popUpToInclusive="true"/>
这几乎符合预期。系统后退按钮和应用栏中的向上图标都不会导航到上一个 fragment 。系统后退按钮退出应用程序。
但是,应用栏中的向上按钮仍然存在,单击它不会按预期执行任何操作。我究竟做错了什么?为什么它还在这里?
在我已经有的主要 Activity 中
AppBarConfiguration config =
new AppBarConfiguration.Builder(navController.getGraph()).build();
NavigationUI.setupActionBarWithNavController(this, navController, config);
和
@Override
public boolean onSupportNavigateUp() {
return navController.navigateUp() || super.onSupportNavigateUp();
}
根据 documentation .
我使用的库版本:
implementation 'android.arch.navigation:navigation-fragment:1.0.0-alpha09'
implementation 'android.arch.navigation:navigation-ui:1.0.0-alpha09'
最佳答案
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.
要解决你的问题,更换
AppBarConfiguration config =
new AppBarConfiguration.Builder(navController.getGraph()).build();
与
AppBarConfiguration config =
new AppBarConfiguration.Builder(R.id.navigation_main, R.id.fragment_main_screen).build();
此处有更多详细信息:AppBarConfiguration
关于android - 导航组件的 popUpTo 不删除向上按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54003666/