rekniyayde
rekniyayde

Reputation: 141

WorkManager does not initialize on demand

I read these instructions: https://developer.android.com/topic/libraries/architecture/workmanager/advanced/custom-configuration#on-demand Now I have a MyApplication class, but I can’t understand how to use it and I can’t find clear instruction anywhere

When requesting WorkManager.getInstance, an error is naturally generated because the MyApplication class is not used anywhere. I could not understand how to activate this class for WorkManager.

AndroidManifest.xml:

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

MyApplication.java:

public class MyApplication extends Application implements Configuration.Provider {
    @NonNull
    @Override
    public Configuration getWorkManagerConfiguration() {
        return new Configuration.Builder().
        setExecutor(Executors.newFixedThreadPool(5)).build();
    }
}

I need to solve this problem exactly, so I will be grateful for the good help. How to make on-demand initialization work?

Upvotes: 3

Views: 1941

Answers (1)

pfmaggi
pfmaggi

Reputation: 6496

WorkManager v2.1 introduced on-demand initialization. This means that you don't need to call WorkManager.initialize() when you have a custom configuration, calling getInstance(Context) is enough.

What is not enough is to call the, now deprecated, getInstance() (without the Context argument). This is stated in the documentation you linked to.

I also covered it at droidcon Berlin 2019 in my talk "Embracing WorkManager". You can find the slides here and the video recording here.

Upvotes: 5

Related Questions