N
N
newdancer2020-06-17 12:33:43
Android
newdancer, 2020-06-17 12:33:43

How to deal with not associated with a fragment manager?

Crashlitics regularly receives an error like
Fatal Exception: java.lang.IllegalStateExceptionFragment SplashFragment{fc97135} (45cab3da-afe1-4c68-ac84-49080333cf2c)} not associated with a fragment manager.
Initially arrived at the sink with findNavController()

Handler().postDelayed({
            findNavController().navigate(R.id.action_splashFragment_to_startFragment)
        }, 2000L)

redid it differently through coroutines, everything the same arrives on findNavController ()
GlobalScope.launch {
            delay(2000L)
            withContext(Dispatchers.Main) {
                val navBuilder = NavOptions.Builder()
                val navOptions: NavOptions =
                    navBuilder.setPopUpTo(R.id.splashFragment, true).setLaunchSingleTop(true).build()
                findNavController().navigate(R.id.startFragment, null, navOptions)
            }
        }

What could be the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
illuzor, 2020-06-17
@iLLuzor

You should not use GlobalScope on Android at all.
Most likely, the timer fires at the moment when the fragment (or activity) has already been killed. It is necessary to take into account its life cycle.
Ideally, move this code to the presenter (ViewModel) and kill the job along with the death of this presenter.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question