https://kotlinlang.org logo
l

luke_c

02/23/2021, 3:20 PM
Could anyone help explain why I am getting a lateinit property has not been initialized error here? We have a Kotlin object that holds some dependencies with an initializer function:
Copy code
object ExampleObjectHolder {
    lateinit var repository: Repository

    fun initialize(client: Client) {
        this.repository = Repository(bffClient)
        this.analytics = analyticsClient
    }
}
We have a top-level function that calls it, and then starts an Activity
Copy code
fun startFlow(host: Activity) {
    ExampleObjectHolder.initialize(ServiceLocator.instance().client)
    host.startActivityForResult(Flow.packIntent(host), REQUEST_CODE)
}
Then inside our Activity we have a ViewModel which gets the dependency from that object
Copy code
private val viewModel by viewModels<FlowViewModel> {
    FlowViewModelFactory(repository = ExampleObjectHolder.repository)
}
This is the point where it’s crashing, but I can’t figure out why because it’s initialized immediately before starting the Activity and the ViewModel accessing it. It’s also only crashing for a very small amount of users.
a

Adam Powell

02/23/2021, 3:32 PM
Where does the initialize call happen for the case when Android relaunches the activity into a cold process container?
l

luke_c

02/23/2021, 4:16 PM
Bingo, that would be why! Thanks for pointing me in the right direction
👍 1
3 Views