我正在将 OnDestinationChangedListener
附加到我的 Activity 中的 NavController
。
它适用于所有 fragment 目标。但是,当我导航到 Activity 目的地时,我的监听器不会收到更改通知。
override fun onStart() {
super.onStart()
navFragment.findNavController().addOnDestinationChangedListener { controller, destination, arguments ->
Log.e("Navigation", "id: ${destination.id} label: ${destination.label}")
}
}
我怎样才能同时监听 Activity 目的地的目的地变化?
最佳答案
Documentation说,那:
The Navigation component is designed for apps that have one main activity with multiple fragment destinations. The main activity is associated with a navigation graph and contains a NavHostFragment that is responsible for swapping destinations as needed. In an app with multiple activity destinations, each activity has its own navigation graph.
所以现在我认为你必须自己跟踪你的 Activity 。 :-( 导航图似乎被“封装”在一个 Activity 中。
关于android - OnDestinationChangeListener 不适用于 Activity 目的地,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55183578/