java.lang.IllegalArgumentException: navigation destination xxx is unknown to this NavController
直接复制我在stackflow中的答案了
It occurs to me when I press back button for two times。At frist,I intercept KeyListener and override KeyEvent.KEYCODE_BACK. I add code below at function named “OnResume” of Fragment. This question is solved.
override fun onResume() {
super.onResume()
view?.isFocusableInTouchMode = true
view?.requestFocus()
view?.setOnKeyListener { v, keyCode, event ->
if (event.action == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK) {
activity!!.finish()
true
}
false
}
}
When it happen to me second time and it`s status is the same as the fisrt one,I find that I maybe use adsurd function.let’s analyze these situations.
Firstly, FragmentA navigate to FragmentB ,then FragmentB navigate to FragmentA ,then press back button ,the crash appear;
Secondly, FragmentA navigate to FragmentB ,then FragmentB navigate to FragmentC ,FragmentC navigate to FragmentA ,then press back button ,the crash appear;
So I think pressing back button, FragmentA will return to FragmentB or FragmentC ,then it cause login mess,finilly,I find that the function of popBackStack can be used of back rather than navigate
NavHostFragment.findNavController(this@TeacherCloudResourcesFragment).
.popBackStack(
R.id.teacher_prepare_lesson_main_fragment,false
)
So far ,the problem is really solved