2

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 ?

droidev
  • 7,352
  • 11
  • 62
  • 94

3 Answers3

0

You probably already have a Workermanager initialized in the Android manifest.

<!-- disable default provider -->
           <provider
            android:name="androidx.work.impl.WorkManagerInitializer"
            android:authorities="${applicationId}.workmanager-init"
            tools:node="remove" />

REF:Custom initialization of Workmanager

Narendra_Nath
  • 4,578
  • 3
  • 13
  • 31
  • Yes, I am disabling the workmanager auto initialisation in manifest as mentioned in the question, cant we handle it together in anyway ? @Narendra_Nath – droidev Oct 19 '22 at 03:57
0

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

droidev
  • 7,352
  • 11
  • 62
  • 94
0

WorkManager and ProcessLifecycleOwner both use androidx.startup. To disable the default initializer for WorkManager without disabling androidx.startup altogether, use this:

 <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>

Documentation

yuval
  • 6,369
  • 3
  • 32
  • 44