DelegatingWorkerFactory


public class DelegatingWorkerFactory extends WorkerFactory


A 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 instance.

Summary

Public constructors

Public methods

final void

Adds a WorkerFactory to the list of delegates.

final ListenableWorker
createWorker(
    @NonNull Context appContext,
    @NonNull String workerClassName,
    @NonNull WorkerParameters workerParameters
)

Override this method to implement your custom worker-creation logic.

Public constructors

DelegatingWorkerFactory

Added in 2.1.0
public DelegatingWorkerFactory()

Public methods

addFactory

Added in 2.1.0
public final void addFactory(@NonNull WorkerFactory workerFactory)

Adds a WorkerFactory to the list of delegates.

Parameters
@NonNull WorkerFactory workerFactory

The WorkerFactory instance.

createWorker

Added in 2.10.0
public final ListenableWorker createWorker(
    @NonNull Context appContext,
    @NonNull String workerClassName,
    @NonNull WorkerParameters workerParameters
)

Override this method to implement your custom worker-creation logic. Use Configuration.Builder.setWorkerFactory to use your custom class.

Throwing an Exception here and no ListenableWorker will be created. If a WorkerFactory is unable to create an instance of the ListenableWorker, it should return null so it can delegate to the default 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.

Parameters
@NonNull Context appContext

The application context

@NonNull String workerClassName

The class name of the worker to create

@NonNull WorkerParameters workerParameters

Parameters for worker initialization

Returns
ListenableWorker

A new ListenableWorker instance of type workerClassName, or null if the worker could not be created