Skip to content

Android WorkManager Enqueue — Complete Guide

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about Android WorkManager Enqueue. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

The Problem

You enqueue a worker but it runs twice, or it doesn't run at all. You try to cancel work but the wrong task gets cancelled because you reused the same name.

Wrong Approach ❌

// Enqueueing without unique identification
WorkManager.getInstance(context)
    .enqueue(OneTimeWorkRequest.from(UploadWorker::class.java))

// Same worker enqueued again from another screen
WorkManager.getInstance(context)
    .enqueue(OneTimeWorkRequest.from(UploadWorker::class.java))

Output: Two identical workers running simultaneously, potentially uploading the same data twice.

Right Approach ✅

val uploadRequest = OneTimeWorkRequestBuilder<UploadWorker>()
    .addTag("upload_photos")
    .build()

// Use unique work for app-wide operations
WorkManager.getInstance(context)
    .enqueueUniqueWork(
        "upload_all_photos",
        ExistingWorkPolicy.REPLACE, // KEEP, APPEND_OR_REPLACE, etc.
        uploadRequest
    )

// Cancelling by tag — precise
WorkManager.getInstance(context).cancelAllWorkByTag("upload_photos")
// Periodic work with unique name
val periodicRequest = PeriodicWorkRequestBuilder<SyncWorker>(15, TimeUnit.MINUTES)
    .build()

WorkManager.getInstance(context)
    .enqueueUniquePeriodicWork(
        "sync_schedule",
        ExistingPeriodicWorkPolicy.KEEP,
        periodicRequest
    )

Output: No duplicate workers. Precise cancellation. Proper periodic scheduling.

Prevention

  • Use enqueueUniqueWork with ExistingWorkPolicy.KEEP or REPLACE.
  • Always use descriptive tags for batch cancellation.
  • Never enqueue periodic work without a unique name.
  • Use beginUniqueWork for chains of unique work.

Common Mistakes with workmanager enqueue

  1. Using head and tail instead of pattern matching, causing runtime errors on empty lists
  2. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
  3. Using return to exit a function early instead of wrapping a pure value in the monad

These mistakes appear frequently in real-world Android code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What is the difference between OneTimeWorkRequest and PeriodicWorkRequest?

OneTimeWorkRequest runs once. PeriodicWorkRequest runs repeatedly at a minimum interval (minimum 15 minutes). Periodic work requires a unique name to avoid duplicates.

### How do I observe work status?

Use WorkManager.getWorkInfoByIdLiveData(workRequest.id) or getWorkInfosByTagLiveData("tag"). Observe the WorkInfo.State for SUCCEEDED, FAILED, ENQUEUED, etc.

### Can I delay a work request?

Yes. Use .setInitialDelay(10, TimeUnit.SECONDS) on the WorkRequestBuilder. The delay is respected even if the app is killed.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro