# DelegatingWorkerFactory

# DelegatingWorkerFactory

Artifact:[androidx.work:work-runtime](https://developer.android.com/jetpack/androidx/releases/work)  
[View Source](https://cs.android.com/search?q=file:androidx/work/DelegatingWorkerFactory.kt+class:androidx.work.DelegatingWorkerFactory)  
Added in[2.1.0](https://developer.android.com/jetpack/androidx/releases/work#2.1.0)

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/androidx/work/DelegatingWorkerFactory "View this page in Java")

<br />

```
open class DelegatingWorkerFactory : WorkerFactory
```

<br />

|---|---|-------------------------------------------------------------------------------------------------------------------------------|
| [kotlin.Any](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html)                                                  |||
| ↳ | [androidx.work.WorkerFactory](https://developer.android.com/reference/kotlin/androidx/work/WorkerFactory)                        ||
|   | ↳ | [androidx.work.DelegatingWorkerFactory](https://developer.android.com/reference/kotlin/androidx/work/DelegatingWorkerFactory) |

*** ** * ** ***

A[WorkerFactory](https://developer.android.com/reference/kotlin/androidx/work/WorkerFactory)which delegates to other factories. Factories can register themselves as delegates, and they will be invoked in order until a delegated factory returns a non-null[ListenableWorker](https://developer.android.com/reference/kotlin/androidx/work/ListenableWorker)instance.

## Summary

|                                                            ### Public constructors                                                            |
|-----------------------------------------------------------------------------------------------------------------------------------------------|
| [DelegatingWorkerFactory](https://developer.android.com/reference/kotlin/androidx/work/DelegatingWorkerFactory#DelegatingWorkerFactory())`()` |

|                                             ### Public functions                                             |
|--------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                | [addFactory](https://developer.android.com/reference/kotlin/androidx/work/DelegatingWorkerFactory#addFactory(androidx.work.WorkerFactory))`(workerFactory: `[WorkerFactory](https://developer.android.com/reference/kotlin/androidx/work/WorkerFactory)`)` Adds a[WorkerFactory](https://developer.android.com/reference/kotlin/androidx/work/WorkerFactory)to the list of delegates.                                                                                                                                                                                                                 |
| `final `[ListenableWorker](https://developer.android.com/reference/kotlin/androidx/work/ListenableWorker)`?` | [createWorker](https://developer.android.com/reference/kotlin/androidx/work/DelegatingWorkerFactory#createWorker(android.content.Context,kotlin.String,androidx.work.WorkerParameters))`(` ` appContext: `[Context](https://developer.android.com/reference/android/content/Context.html)`,` ` workerClassName: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`,` ` workerParameters: `[WorkerParameters](https://developer.android.com/reference/kotlin/androidx/work/WorkerParameters) `)` Override this method to implement your custom worker-creation logic. |

## Public constructors

### DelegatingWorkerFactory

Added in[2.1.0](https://developer.android.com/jetpack/androidx/releases/work#2.1.0)  

```
DelegatingWorkerFactory()
```  

## Public functions

### addFactory

Added in[2.1.0](https://developer.android.com/jetpack/androidx/releases/work#2.1.0)  

```
fun addFactory(workerFactory: WorkerFactory): Unit
```

Adds a[WorkerFactory](https://developer.android.com/reference/kotlin/androidx/work/WorkerFactory)to the list of delegates.  

|                                                  Parameters                                                  |
|--------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------|
| `workerFactory: `[WorkerFactory](https://developer.android.com/reference/kotlin/androidx/work/WorkerFactory) | The[WorkerFactory](https://developer.android.com/reference/kotlin/androidx/work/WorkerFactory)instance. |

### createWorker

```
final fun createWorker(
    appContext: Context,
    workerClassName: String,
    workerParameters: WorkerParameters
): ListenableWorker?
```

Override this method to implement your custom worker-creation logic. Use[Configuration.Builder.setWorkerFactory](https://developer.android.com/reference/kotlin/androidx/work/Configuration.Builder#setWorkerFactory(androidx.work.WorkerFactory))to use your custom class.

Throwing an[Exception](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-exception/index.html)here and no[ListenableWorker](https://developer.android.com/reference/kotlin/androidx/work/ListenableWorker)will be created. If a[WorkerFactory](https://developer.android.com/reference/kotlin/androidx/work/WorkerFactory)is unable to create an instance of the[ListenableWorker](https://developer.android.com/reference/kotlin/androidx/work/ListenableWorker), it should return`null`so it can delegate to the default[WorkerFactory](https://developer.android.com/reference/kotlin/androidx/work/WorkerFactory).

Returns a new instance of the specified`workerClassName`given the arguments. The returned worker must be a newly-created instance and must not have been previously returned or invoked by WorkManager. Otherwise, WorkManager will throw an[IllegalStateException](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html).  

|                                                      Parameters                                                       |
|-----------------------------------------------------------------------------------------------------------------------|----------------------------------------|
| `appContext: `[Context](https://developer.android.com/reference/android/content/Context.html)                         | The application context                |
| `workerClassName: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)                  | The class name of the worker to create |
| `workerParameters: `[WorkerParameters](https://developer.android.com/reference/kotlin/androidx/work/WorkerParameters) | Parameters for worker initialization   |

|                                               Returns                                                |
|------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [ListenableWorker](https://developer.android.com/reference/kotlin/androidx/work/ListenableWorker)`?` | A new[ListenableWorker](https://developer.android.com/reference/kotlin/androidx/work/ListenableWorker)instance of type`workerClassName`, or`null`if the worker could not be created |