I have a very strange problem with Compose and App...
# compose
p
I have a very strange problem with Compose and Application class. I'm accesing the applicationContext of my custom Application class on the init block of the first viewmodel of the first screen on the first activity of the app, and it's returning null. I'm initializing it on the Application's onCreate method. It can be a problem related with Compose. Supposedly Application's onCreate is
Called when the application is starting, before any activity, service, or receiver objects (excluding content providers) have been created
Copy code
class CustomApplication: Application() {
    lateinit var container: AppContainer
    lateinit var context: Context

    override fun onCreate() {
        super.onCreate()
        container = AppContainer()
        context = applicationContext
    }
}
I get the Application object using this:
Copy code
fun CreationExtras.CustomApplication(): CustomApplication=
    (this[ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY] as CustomApplication)
z
I don’t know much about how the ViewModel system works, but if you don’t get a response here I would file a bug to the Google tracker (link in channel topic)
e
What does
AppContainer()
do? Is that a DI container? Are you sure some chain of instantiations is not somehow requiring your
context
at that point?
m
It can be a problem related with Compose
I do not see how. Could you please explain? FWIW, this was cross-posted in Stack Overflow. I pointed out that `getApplicationContext()` can return `null` , notably if the process was started for the purposes of auto backup.