HiltWorkerFactory

public final class HiltWorkerFactory extends WorkerFactory


Worker Factory for the Hilt Extension

A provider for this factory will be installed in the dagger.hilt.components.SingletonComponent.

Summary

Public methods

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

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

Inherited methods

From androidx.work.WorkerFactory
final ListenableWorker
createWorkerWithDefaultFallback(
    Context appContext,
    String workerClassName,
    WorkerParameters workerParameters
)

Returns a new instance of the specified workerClassName given the arguments.

Public methods

createWorker

Added in 1.0.0
public @Nullable 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 will crash the application. 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
@Nullable ListenableWorker

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