WorkManager : getInstance() is Deprecated
Asked Answered
S

2

11

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?

Spittoon answered 26/7, 2019 at 7:39 Comment(2)
I am sharing this thing for knowledge purpose. Google gives results of stackoverflow before official websites. Do let me know why down-vote?Spittoon
I'm OK having a question like this here if it helps developers. (upvoted it) However, searching for "WorkManager.getInstance() is Deprecated in version 2.1.0" returns links to the release notes and to the documentation. I usually try to avoid duplication in these cases, because it can be a maintenance cost in the future. If you think that WorkManager's documentation needs improvement, please file a bug: issuetracker.google.com/issues/…Caracole
S
30

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

Spittoon answered 26/7, 2019 at 7:39 Comment(0)
B
6

According to document, you must use this way:

val workManager = WorkManager.getInstance(applicationContext)
Backplate answered 30/9, 2020 at 17:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.