Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WorkManager : getInstance() is Deprecated

WorkManager.getInstance() is Deprecated in version 2.1.0

dependency :

implementation 'androidx.work:work-runtime:2.1.0'

What are the changes in this method or any other ways?

like image 455
Pratik Butani Avatar asked Jul 26 '19 07:07

Pratik Butani


People also ask

How do I cancel WorkManager on Android?

Stop a running Worker You explicitly asked for it to be cancelled (by calling WorkManager. cancelWorkById(UUID) , for example). In the case of unique work, you explicitly enqueued a new WorkRequest with an ExistingWorkPolicy of REPLACE . The old WorkRequest is immediately considered cancelled.

Does WorkManager run on background thread?

Worker is the simplest implementation, and the one you have seen in previous sections. WorkManager automatically runs it on a background thread (that you can override).


1 Answers

I found the solution on documentation as follow:

This method is deprecated.

Call getInstance(Context) instead.

Where Context is used for on-demand initialization.

WorkManager v2.1 introduces a new way to customize its configuration. While with the previous version it was necessary to create a new configuration and initialize WorkManager during the app startup, v2.1 adds a new "on demand" initialization. This means that WorkManager is initialized (with the default or the custom) initialization the first time that the app calls the getInstance(Context) method.

The Context in this case is used to retrieve the application object and see if it implements the Configuration.Provider interface.

More information are available in WorkManager's documentation on custom configuration.

This change is documented in WorkManager's release notes (this was introduced in WorkManager v2.1-alpha01) and it's explained there why it's better to use the new getInstance(Context) method even if you're not using on demand initialization.

Note : Whenever you change or update dependency version, please go through the release notes

like image 63
Pratik Butani Avatar answered Oct 01 '22 16:10

Pratik Butani