简体   繁体   English

如何使用导航组件获取上一个目的地?

[英]How to get previous destination with navigation component?

I have fragments A, B, C, and X where their navigations are as the following.我有片段 A、B、C 和 X,它们的导航如下。

 A --> X 
 B --> X 
 C --> X

How could I know the information of the last or previous destination in X?我怎么知道X中最后一个或上一个目的地的信息?

The current destination ID can be found by可以通过以下方式找到当前的目的地 ID

Navigation.findNavController(v).getCurrentDestination().getId()

I also tried using getBackStackEntry(int destinationId) but it returns only the topmost NavBackStackEntry for a destination id.我也尝试使用getBackStackEntry(int destinationId)但它只返回目标 id 的最顶层 NavBackStackEntry。 In other words, the current destination.换句话说,当前目的地。

Also getActivity().getSupportFragmentManager().getBackStackEntryCount();还有getActivity().getSupportFragmentManager().getBackStackEntryCount(); returning 0.返回 0。

Possible solutions are:可能的解决方案是:

  1. Pass data from A, B, and C to X and identify the previous fragment by checking with that data.将数据从 A、B 和 C 传递给 X,并通过检查该数据来识别前一个片段。
  2. Use shared SharedViewModel.使用共享的 SharedViewModel。

Is it possible to get the current destination?是否有可能获得当前目的地?

Java: Navigation.findNavController(v).getPreviousBackStackEntry().getDestination().getId(); Java: Navigation.findNavController(v).getPreviousBackStackEntry().getDestination().getId();

Kotlin: findNavController().previousBackStackEntry?.destination?.id Kotlin: findNavController().previousBackStackEntry?.destination?.id

This should return you the previous destination's id in the 'X' fragment.这应该会在“X”片段中返回上一个目的地的 id。

add destination change listener.添加目标更改侦听器。 and compare id with A,B,C of ids on back key press并在后退键按下时将 id 与 id 的 A、B、C 进行比较

NavController.OnDestinationChangedListener { controller, destination, arguments ->
    // react on change or 
    //you can check destination.id or destination.label and act based on that
)}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 导航组件:如何在使用深度链接导航到目的地后弹出回到之前的目的地? - Navigation Component: How to pop back to previous destination after navigate to a destination using deep link? 如何使用Android导航组件将值传递回之前的片段目的地? - how to pass a value back to previous fragment destination using Android navigation component? Android 导航组件:如何以编程方式添加目的地? - Android Navigation Component: How to add a destination programmatically? 如何使用Android中的导航控制器以编程方式返回上一个目的地? - how to back to previous destination programatically using navigation controller in Android? 如果使用导航组件,如何更新上一个屏幕? - How to Update the previous screen if using navigation component? 如何使用 Android 导航组件和深度链接打开活动和特定目的地 - How to open an activity and a particular destination using Android navigation component and deeplink Android 导航组件 - 如何“popBackStack”到不存在的目的地 - Android Navigation Component - How to "popBackStack" to non-existing Destination 如何将 function 作为参数传递给目标片段 - Android 导航组件 - How to pass a function as an argument to a destination fragment - Android Navigation Component 如果我使用导航组件单击导航抽屉中的菜单,如何执行操作而不是移动到另一个目的地? - how to perform an action instead of moving to another destination if I click a menu in Navigation Drawer using Navigation Component? 使用导航组件的多个后退堆栈 - 如何始终 go 开始特定底部导航选项卡的目的地? - Multiple back stacks using navigation component - How to always go to start destination for specific bottom navigation tab?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM