导航DESTINATION_NAME对于此NavController未知,是否正在重新打开以前使用navController.popBackStack()关闭的片段? [英] Navigation DESTINATION_NAME is unknown to this NavController, ReOpening fragment previously closed using navController.popBackStack()?
问题描述
我在我的应用程序中使用了导航组件,最近它可以正常工作,但是将项目更新为AndroidX
后,只有先前那个目的地(我要打开)才收到错误navigation destination DESTINATION_NAME is unknown to this NavController
使用navController.popBackStack()
从自身关闭.另外,如果我从MainActivity
关闭DESTINATION片段,也没有错误,但是使用popBackStack
从自身关闭了仅错误片段.像下面的
I am using Navigation Component in my app , recently it was working correctly but after updating project to AndroidX
I am getting error navigation destination DESTINATION_NAME is unknown to this NavController
only if that destination(Which I'm going to open) is previously closed from itself using navController.popBackStack()
. Also, There is no error if I close DESTINATION fragment from MainActivity
, But Error only Occurs fragment is closed from itself using popBackStack
. like below
DestinationFragment
viewModelOfActivity.handleBackButton.observe(this, Observer {
Navigation.findNavController(requireActivity(), R.id.main_nav_host).popBackStack()
//CALLING popBackStack() HERE CAUSING PROBLEM WHEN REOPNING THIS DESTINATION(or frg ) AGIAN
})
MainActivity
override fun onBackPressed() {
if (myViewModel.isDefaultBehaviour.value == true) {
super.onBackPressed()
} else{
myViewModel.handleBackButton.value=true
//NO ERROR IF HANDLE BACK BUTTON HERE ie->findNavController(R.id.main_nav_host).popBackStack()
//INSTEAD OF myViewModel.handleBackButton
}
}
我还检查了相关问题,但没有帮助类似问题.
I Have also checked related Question but no help Similar Question.
注意:我正在使用最新版本的导航库(alpha05)
NOTE: I am using the latest version of the navigation library (alpha05)
推荐答案
I was using SingleLiveEvent in DestinationFragment to observe back press from MainActivity as I have already mentioned this in my question. So the problem was in SingleLiveEvent
I noticed that I have accidently changed code of fun observe(owner: LifecycleOwner, observer: Observer<in T>)
to
override fun observe(owner: LifecycleOwner, observer: Observer<in T>) {
super.observe(owner, observer)//Here is problem I was calling super twice in function
if (hasActiveObservers()) {
Log.w(TAG, "Multiple observers registered but only one will be notified of changes.")
}
super.observe(owner, Observer { t ->/** other code*//})
}
在这里您可以看到我两次调用了super
函数,该函数在Fragment
中两次调用了观察者的onChanged
,下面的代码被调用了两次
Navigation.findNavController(requireActivity(), R.id.main_nav_host).popBackStack()
popBackStack()
两次.
然后,我更改了observe
函数,如下所示
Here you can see I was calling super
function twice which calls onChanged
of observer twice in Fragment
, below code is called twice
Navigation.findNavController(requireActivity(), R.id.main_nav_host).popBackStack()
which popBackStack()
twice.
Then I have changed observe
function like below
@MainThread
override fun observe(owner: LifecycleOwner, observer: Observer<in T>) {
if (hasActiveObservers()) {
Log.w(TAG, "Multiple observers registered but only one will be notified of changes.")
}
super.observe(owner, Observer { t ->/** other code*//})
}
现在我的代码可以正常工作
Now my code works fine
这篇关于导航DESTINATION_NAME对于此NavController未知,是否正在重新打开以前使用navController.popBackStack()关闭的片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!