0

I have implemented LifecycleObserver in my Application class and it works without issue before. Now I needed to add WorkManager workers and inject them using hilt so I have this in my AndroidManifest.xml file for workers to work with Hilt.

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

After adding this in my manifest file, the callbacks for LifecycleObserver in my Application class are not triggered. I know there might be some conflict with both of this working together but I haven't found any solution to make this work.

Aplication Class

@HiltAndroidApp
class Application : CustomLocalizationApplication(), LifecycleObserver, androidx.work.Configuration.Provider {
    override fun onCreate() {
        super.onCreate()
        ...
        ProcessLifecycleOwner.get().lifecycle.addObserver(this)
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    fun onMoveToForeground() {
        // app moved to foreground
        // not called anymore after adding code snippet in Manifest

        playerBackgroundController.resume()
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    fun onMoveToBackground() {
        // app moved to background
        // not called anymore after adding code snippet in Manifest

        playerBackgroundController.pauseIfBackground()
    }

    override fun getWorkManagerConfiguration(): androidx.work.Configuration {
        return androidx.work.Configuration.Builder()
            .setMinimumLoggingLevel(Log.INFO)
            .setWorkerFactory(workerFactory)
            .build()
    }
}
Ralph
  • 550
  • 3
  • 10
  • 25

1 Answers1

0

It's helped me. Replace this code in your AndroidManifest.xml:

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

with this:

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

    <meta-data
        android:name="androidx.work.WorkManagerInitializer"
        android:value="androidx.startup"
        tools:node="remove" />
</provider>
SerjantArbuz
  • 982
  • 1
  • 12
  • 16