My problem was that I needed to log something and it wasn't called Application onCreate().
If you have the same problem and need some Side Effects to trigger like Analytics events.
Use these 2 approaches.
Don't put your main logic (for example don't initialize the libraries)
in the below places.
Approach 1: Override onCreate of your Application Class like below:
@OptIn(DelicateCoroutinesApi::class)
override fun onCreate() {
GlobalScope.launch {
ProcessLifecycleOwner.get().lifecycle.withCreated {
// Here onCreate of the Application will be called !
}
}
super.onCreate()
}
Approach 2: Create a class for example named AppLifecycleListener that implements DefaultLifecycleObserver and override required methods.
@Singleton
class AppLifecycleListener @Inject constructor() : DefaultLifecycleObserver {
override fun onCreate(owner: LifecycleOwner) {
super.onCreate(owner)
// Here onCreate of the Application will be called !
}
}
Don't forget to addObserver() your class in Application
@Inject
lateinit var appLifecycleListener: AppLifecycleListener
override fun onCreate() {
super.onCreate()
ProcessLifecycleOwner.get().lifecycle.addObserver(appLifecycleListener)
}