在导航组件中,如何检测片段是否从弹出事件中前往前台?
我从A到B,现在我使用返回键关闭了B,它返回到A,在A中(在onViewCreated事件中),我想检测它是来自B的。
findNavController((R.id.nav_host_fragment)
.addOnDestinationChangedListener{ hostController, destination, _ ->
val push = currentBackStackSize < hostController.bacStack.size // else pop
// Then save current backstack size
}
false
的导航参数(在nav_graph.xml中)。onCreate()
中添加以下内容。requireActivity().onBackPressedDispatcher.addCallback {
val action = BDirections.actionBFragmentAFragment(true)
findNavController().navigate(action)
}