Search code examples
androidandroid-workmanager

Is it possible to add a tag to a Worker within it?


I want to add a tag to a Worker within it (not via a Builder).
I've tried doing that the following way:

class MyWorker(...): Worker(...) {
  init {
    tags.add("MY_TAG")
  }
}

But it doesn't seem to work.


Solution

  • Tags are accessible in a Worker through its WorkParameters but you can only read them with WorkerParameters#getTags().

    It is not possible to add tags to an existing WorkRequest

    Maybe you can use other methods to add some information to a Worker like the ListenableWorker#setProgressAsync(androidx.work.Data). But it really depends on what are you trying to achieve.