Android WorkManager: background work that survives restarts
Short answer: WorkManager is Android's recommended library for background work that must finish even if the user leaves the app or the phone restarts, such as uploading photos, syncing orders or sending queued form submissions. You describe the work, the conditions it needs (like a network connection) and how to retry, and WorkManager schedules it with the system. Use unique work names to avoid duplicates, return a retry result for temporary failures, and keep long tasks visible with a foreground notification.
The problem with doing it yourself
Business apps often need to finish something after the user has moved on: a delivery driver's proof-of-delivery photos, a field technician's job report, a sales rep's offline orders. Code that starts a background thread when the user taps "Submit" works in testing, then fails in real life because:
- Android stops background processes to save battery and memory.
- The phone loses signal halfway through.
- The user swipes the app away or the device restarts.
Android's background work documentation describes WorkManager as the recommended solution for persistent work, with scheduling that lets tasks persist across app restarts and device reboots. That's exactly the guarantee you want for anything a customer or employee assumes has been sent.
The three kinds of work
The documentation groups persistent work into three types:
- Immediate: must start now and finish soon. It can be marked expedited, which asks the system to run it quickly.
- Long running: may run longer than about ten minutes, such as a large batch upload. These run as foreground work, with a visible notification.
- Deferrable: can run later or repeatedly, such as a periodic sync.
Choosing the right type matters because the system treats them differently, particularly on newer Android versions with stricter background limits.
Set constraints instead of checking yourself
Rather than checking for Wi-Fi or battery inside your code, declare constraints on the work request. WorkManager's documentation gives examples like running only on an unmetered network, when the device is idle, or when the battery isn't low. The work waits until the constraints are met, then runs.
For a field app uploading large photo sets, that might mean "any network" for urgent job reports and "unmetered network and charging" for bulk image backups.
Retries with backoff
Temporary failures are normal: the server is busy, the connection drops. If your worker returns Result.retry(), WorkManager reschedules it using a backoff delay and policy, according to the documentation on defining work. You choose linear or exponential backoff and a starting delay.
Use retry only for failures that might succeed later. If the server rejects the data as invalid, return failure and record why, rather than retrying the same bad request repeatedly.
Avoid duplicates with unique work
A common bug is enqueueing the same sync several times, for example every time the app opens. WorkManager supports unique work: you give the request a name and choose what happens if work with that name already exists, such as keeping the existing one or replacing it. For things like "sync orders", unique work stops a pile-up of identical jobs.
Every request also has its own ID, and you can add tags to group related work, so you can observe or cancel it later.
Periodic work has limits
For regular syncs, WorkManager offers periodic work. The documentation states that the minimum repeat interval is 15 minutes, the same as Android's JobScheduler. If you need something more frequent, such as live order tracking, background polling isn't the right tool; push messages from your server or a foreground service are better fits.
Periodic timing is approximate. The system batches work to save battery, so a 15-minute job won't run at exactly 15-minute intervals.
Show progress for long tasks
When a driver uploads 40 photos, they want to know it's happening. For long-running work, the documentation describes calling setForeground() from the worker so it runs as foreground work with a notification. Combine that with progress updates the app can observe, so the screen shows "12 of 40 uploaded" when the user returns.
Also tell the user what's queued. A small "3 reports waiting to upload" banner prevents duplicate submissions and support calls.
Make the work itself safe to repeat
Because WorkManager may run work more than once, for example after a retry or a process restart mid-task, design each worker so repeating it is harmless:
- Send an idempotency key or unique submission ID with uploads, so the server can ignore duplicates.
- Upload in chunks and track which parts succeeded.
- Store pending data in a local database rather than in memory, and delete it only after the server confirms receipt.
Testing
WorkManager includes testing support for running workers synchronously in tests. Also test on a real device with:
- Airplane mode switched on mid-upload.
- The app swiped away during work.
- A device restart with work pending.
- Battery saver switched on.
Each should end with the work completing once, not zero times or twice.
A worked example
Picture a hypothetical Hamilton plumbing company whose technicians' app uploaded job photos and signatures when they tapped "Complete". On rural jobs with poor signal, uploads often failed silently, and the office chased missing paperwork. The team stored completed jobs in a local database, enqueued unique upload work per job with a network constraint and exponential backoff, used foreground work for large photo sets and showed a pending-uploads banner. Missing reports stopped, and technicians could see when everything had gone through.
FAQ
Is WorkManager only for Kotlin apps?
No. It works with Kotlin and Java, and several cross-platform frameworks have plugins that use it on Android.
Does work run if the app is force-stopped?
A force stop from system settings cancels scheduled work until the app is opened again. A normal swipe-away doesn't.
What's the iPhone equivalent?
iOS has its own background task APIs with different rules. Cross-platform apps need separate handling for each platform.
Can I use it for exact-time alarms?
No. It's for guaranteed but flexible timing. Exact alarms need other Android APIs.
If your Android app loses uploads or syncs when connections drop, tell us what needs to get through.