lateinit var is not initialized

81 Views Asked by At

I am confused as to why this lateinit var koinApp is sometimes not initialized...

object MyKoin {
    lateinit var koinApp: KoinApplication
}

class Builder {
    init {
        MyKoin.koinApp = koinApplication {
            assertMainThread() // Succeeds
        }
    }

    fun build() = MyFragment()
}

class MyFragment()  : Fragment() {
    private val injectedProperty by MyKoin.koinApp.inject<Something>() // Sometimes crashes with "lateinit var MyKoin.koinApp not initialized"
}

And is used like this:

class TestFragment {
    private val rootViewId = View.generateViewId()

    override fun onCreate(savedInstanceState: Bundle?) {
        val myFragment = Builder.build()
        childFragmentManager.commit {
            replace(rootViewId, fragment)
        }
    }
}

There is absolutely no way that MyFragment is running the code injectedProperty before Builders init executes.... is there?

0

There are 0 best solutions below