lundi 8 juin 2020

Android: Why is "NavController Not Set" when testing?

In my Fragment, I have this sort of simplified logic where I get the NavController:

late init var navController: NavController

override fun onViewCreated() {
    navController = findNavController()
}

This works fine when I launch my application, however when I follow the steps listed at https://developer.android.com/guide/navigation/navigation-testing I get an error of like:

java.lang.IllegalStateException: ... does not have a NavController set. 

I followed the Android documentation exactly and even mocked the NavController. After further research it seems that the test sets up the NavController earlier than when I set it up in the onViewCreated.

If I moved my instantiation of the navcontroller in my production code to "onCreateView" my test works but my production code fails because the "View" is not yet attached. How do I fix this dilemma?

Aucun commentaire:

Enregistrer un commentaire