Fragment XXX {} not associated with a fragment manager Fragment XXX {} not associated with a fragment manager android android

Fragment XXX {} not associated with a fragment manager


The same issue was happening to me, I managed to solve it by wrapping the navigate method inside a view?.post call like so:

view?.post {    findNavController().navigate(SplashFragmentDirections.actionSplashFragmentToLoginFragment())}


The problem is caused when you try to call findNavController() on a fragment that has been detached, if you're using Kotlin you can create an extension function like so

fun Fragment.findNavControllerSafely(): NavController? {    return if (isAdded) {        findNavController()    } else {        null    }}

then use it in any fragment

findNavControllerSafely()?.navigate(/*pass here the nav directions>*/)

you can also surround it with try/catch but I do not recommend this as it will silently catch/ignore other exceptions that might be useful, try navigating through the source code of findNavController() to get a better feel of the kind of exceptions that are thrown


for me the problem was the fragment after restoration because of config change was the fragment manager was not valid in fragment so I had to get it directly from activities like this:

activity?.findNavController(R.id.nav_host_fragment)?.navigate(....)

or

activity?.supportFragmentManager?.setFragmentResultListener(.....)