IllegalArgumentException: navigation destination xxx is unknown to this NavController

后端 未结 30 2379
遇见更好的自我
遇见更好的自我 2020-11-27 11:28

I am having issue with the new Android Navigation Architecture component when I try to navigate from one Fragment to another, I get this weird error:

30条回答
  •  情话喂你
    2020-11-27 12:10

    It occurs to me when I press the back button two times. At first, I intercept KeyListener and override KeyEvent.KEYCODE_BACK. I added the code below in the function named OnResume for the Fragment, and then this question/issue 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 happens to me for a second time, and it's status is the same as the first one, I find that I maybe use the adsurd function. Let’s analyze these situations.

    1. Firstly, FragmentA navigates to FragmentB ,then FragmentB navigates to FragmentA, then press back button... the crash appears.

    2. Secondly, FragmentA navigates to FragmentB, then FragmentB navigates to FragmentC, FragmentC navigates to FragmentA, then press back button... the crash appears.

    So I think when pressing back button, FragmentA will return to FragmentB or FragmentC, then it causes the login mess. Finally I find that the function named popBackStack can be used for back rather than navigate.

      NavHostFragment.findNavController(this@TeacherCloudResourcesFragment).
                            .popBackStack(
                                R.id.teacher_prepare_lesson_main_fragment,false
                            )
    

    So far, the problem is really solved.

提交回复
热议问题