Search code examples
androidandroid-workmanagerdagger-hilt

No DefaultLifecycleObserver callback when using work manger OnDemand initialization


I need to observe application lifecycle to hen app goes to background and comes to foreground but since I added the OnDemand initialization for the workmanger the callbacks for DefaultLifecycleObserver does not work anymore.

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

If i remove above line it works well but then Worker cannot be initialize. I'm using JAVA 8 and hilt for dependency injection.


Solution

  • According to the custom WorkManager configuration guide, you can disable WorkManager initialization while keeping any other App Startup components that don't depend on WorkManager:

     <provider
        android:name="androidx.startup.InitializationProvider"
        android:authorities="${applicationId}.androidx-startup"
        android:exported="false"
        tools:node="merge">
        <!-- If you are using androidx.startup to initialize other components -->
        <meta-data
            android:name="androidx.work.WorkManagerInitializer"
            android:value="androidx.startup"
            tools:node="remove" />
     </provider>