- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经通过 navigation_view.setupWithNavController(navController)
连接了我的菜单 xml 和 Android 导航组件,并且一切正常。我的菜单的一部分是注销选项。如果用户选择此导航目的地,应用程序应导航到 AuthorizationFragment
并将返回堆栈弹出到此 Fragment 并将 inclusive 设置为 true 以删除所有先前的 fragment。
我查看了导航库的代码,发现应该发生这种情况:
NavigationUI.onNavDestinationSelected():
/**
* Attempt to navigate to the {@link NavDestination} associated with the given MenuItem. This
* MenuItem should have been added via one of the helper methods in this class.
*
* <p>Importantly, it assumes the {@link MenuItem#getItemId() menu item id} matches a valid
* {@link NavDestination#getAction(int) action id} or
* {@link NavDestination#getId() destination id} to be navigated to.</p>
* <p>
* By default, the back stack will be popped back to the navigation graph's start destination.
* Menu items that have <code>android:menuCategory="secondary"</code> will not pop the back
* stack.
*
* @param item The selected MenuItem.
* @param navController The NavController that hosts the destination.
* @return True if the {@link NavController} was able to navigate to the destination
* associated with the given MenuItem.
*/
public static boolean onNavDestinationSelected(@NonNull MenuItem item,
@NonNull NavController navController) {
NavOptions.Builder builder = new NavOptions.Builder()
.setLaunchSingleTop(true)
.setEnterAnim(R.anim.nav_default_enter_anim)
.setExitAnim(R.anim.nav_default_exit_anim)
.setPopEnterAnim(R.anim.nav_default_pop_enter_anim)
.setPopExitAnim(R.anim.nav_default_pop_exit_anim);
if ((item.getOrder() & Menu.CATEGORY_SECONDARY) == 0) {
builder.setPopUpTo(findStartDestination(navController.getGraph()).getId(), false);
}
NavOptions options = builder.build();
try {
//TODO provide proper API instead of using Exceptions as Control-Flow.
navController.navigate(item.getItemId(), null, options);
return true;
} catch (IllegalArgumentException e) {
return false;
}
}
所以我的问题是,是否有一种方法可以覆盖此方法并构建 NavOptions,这些 NavOptions 将返回堆栈弹出到目的地 ID,并将包含选项设置为 true?
最佳答案
像往常一样,为问题睡一觉会有所帮助。我发现您可以轻松设置自己的 NavigationItemSelectedListener
。另外,我在问题中提到的方法是 public static,因此它也可以很容易地称为回退功能。
这是我的代码:
navigation_view.setNavigationItemSelectedListener { item: MenuItem ->
// check for my special case where I want to pop everything
// from the backstack until we reach the login fragment
if (item.itemId == R.id.fragment_auth) {
val options = NavOptions.Builder()
.setPopUpTo(navController.currentDestination!!.id, true)
.setLaunchSingleTop(true)
.build()
navController.navigate(R.id.action_global_authFragment, null, options)
true // return true -> we handled this navigation event
} else {
// Fallback for all other (normal) cases.
val handled = NavigationUI.onNavDestinationSelected(item, navController)
// This is usually done by the default ItemSelectedListener.
// But there can only be one! Unfortunately.
if (handled) drawer_layout.closeDrawer(navigation_view)
// return the result of NavigationUI call
handled
}
}
关于android - 拦截 NavigationUI.onNavDestinationSelected() 使 backstack 弹出 "inclusive = true",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57529211/
简短版本: 我有 5 个 fragment ,均由 replace 添加到容器中方法(A -> B -> C -> D -> E),现在我想从我的backStack(C)中删除第3个 fragment
我正在开发一个 Android 应用程序,遇到了一个涉及 Fragments 和 FragmentTransactions 的错误。我创建了一个示例应用程序来演示该问题。这是我正在做的: add Fr
在两个 fragment 上使用导航组件,我的应用程序当前在 navigateUp() 时销毁了一个 fragment 被调用(使用 setupActionBarWithNavController()
我在做navigation component codelab。 在10部分中,我们添加了Deeplink小部件。 我将目的地替换为R.id.flow_step_two_dest val cust
我无法解决 BackStack 的问题。我有 3 个由 Activity 管理的 fragment : 我使用这段代码的 fragment 之间的导航: private void showFragme
过去几天我一直在处理一个问题,但似乎没有找到答案。 描述:我有一个包含抽屉导航的主要 Activity 。抽屉导航的每个项目(单击时)都会创建一个 fragment 。在该 fragment 中,有一
对于我的应用程序,某些 fragment 要求用户在显示之前在对话框中输入 PIN。我有一个 BaseFragment 类,如果需要 pin,我的所有其他 fragment 都会扩展该类。我现在的问题
我有一个包含单个 Activity 的应用程序,该 Activity 使用菜单抽屉进行导航。每次单击菜单抽屉中的项目时,我都会用一个新 fragment 替换显示的 fragment 并将其添加到后台
我试图在调用方法时加载新 fragment 。此方法创建一个新 fragment 并“替换”另一个 fragment : private void showTestFragment(Fragment
您好,如何清除 fragment 回栈我使用下面的逻辑它不起作用... for(int i = 0; i < mFragmentManager.getBackStackEntryCount(); ++
我的导航图有 auth包含 LoginFragment 和 SignUpFragment 的子图: [...] 我想检查这两个目的地中的任何一个当前是否在后台。但不是单独检查每个
我的 ViewPager 有 3 个选项卡。 Tab(3) 包含 2 个 fragment (A) --> (B)。 Tab(1) Tab(2) Tab(3) --> (B) --> (C) 我在Ta
我不确定为什么这不起作用,我查看了文档,看起来这应该可行。 DetailFragment newFragment = new DetailFragment(); FragmentTransaction
我想检查 BackStack 是否为空。我有这样的东西: List l2 = getFragmentManager().getFragments(); if(l2 == null){
概览 您好,我目前正在尝试开发一款采用新 Material 设计和兼容 API15 的 Android 应用程序。这次我想通过在 Activity 中替换它们来仅使用一个 Activity 和多个 f
我在后台堆栈中有 fragment ,这些 fragment 是使用 addtobackstack 函数添加的。当我在不使用 addtobackstack 函数的情况下添加一个 fragment 然后
当用户点击状态栏中的通知时,我正在使用 FCM 显示 fragment 。 当前的应用程序流是这个 MainActivity( fragment A -> fragment B -> fragment
在我的 Activity 中,我正在加载 fragment 并在用户单击项目时将它们添加到后台堆栈。这工作正常,后台加载也很好。 不幸的是,当我旋转设备时, Activity 是空白的。我已经在 SO
我有一个 FragmentActivity,当用户在应用程序中导航时,我用用户选择的 fragment 替换当前 fragment ,并将事务添加到后台堆栈。一切正常,用户可以通过按后退按钮返回到之前
我的 Activity 中有以下代码: public void categoryClicked(int categoryId, String categoryName) {
我是一名优秀的程序员,十分优秀!