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.
NOTE: I am using the latest version of the navigation library (alpha05)
The previous value likely still exists in the view model and is triggering right away. I'd recommend using an interface to handle your back button delegation instead of an observer. That should fix the usage.
What's happening is you're popping too far up your back stack to the point where you don't have an active graph anymore. This is happening because your observer is getting triggered more often than it should. To see this, I'd recommend debugging that line and inspecting the graph right before a crash. It's likely to be null.
i am use this way
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 offun observe(owner: LifecycleOwner, observer: Observer<in T>)
toHere you can see I was calling
super
function twice which callsonChanged
of observer twice inFragment
, below code is called twiceNavigation.findNavController(requireActivity(), R.id.main_nav_host).popBackStack()
whichpopBackStack()
twice.Then I have changed
observe
function like belowNow my code works fine
I had the same issue. In my app there were three fragments
A -> B -> C
and I had to return toA
fromC
. In my case I usedpopBackStack()
to close the current fragment and return to the previous one, after that any attempt to navigate fromA
threwNavigation xxx is unknown to this NavController
exception.I ended up using
navigate()
toA
instead of pop fragments from back stack. UsingpopUpTo
andpopUpToInclusive
settings properly fixed the issue (see docs https://developer.android.com/guide/navigation/navigation-getting-started#popupto_example_circular_logic)