Search code examples
androidandroid-contentproviderandroid-lifecycleandroid-workmanager

ProcessLifecycleOwner is not giving lifecycle callback in Application class


I am using ProcessLifecycleOwner.get().lifecycle.addObserver(this) in my Application class, and I expected to get onStateChanged() callback to be called, however I have added a provider in my manifest for disabling the standard automated initialisation of WorkManager.

 <provider
  android:name="androidx.startup.InitializationProvider"
  android:authorities="${applicationId}.androidx-startup"
  tools:node="remove">
 </provider>

From couple of other solutions listed in SO, I have modified the provider as follows

And I can see the first onStateChanged() callback with event ON_CREATE and immediately the app crashes. and the crash log is given below

Caused by: java.lang.IllegalStateException: WorkManager is already initialized. Did you try to initialize it manually without disabling WorkManagerInitializer

Does anyone has worked on such thing, suggest any working approaches ?


Solution

  • I found the solution, I have created another package androix/lifecycle and added a new class and initialised the ProcessLifeCycleOwner as follows

    
    fun initialize(context: Application): LifecycleOwner {
            LifecycleDispatcher.init(context)
            ProcessLifecycleOwner.init(context)
            return ProcessLifecycleOwner.get()
        }
    

    And that worked perfect