tezvyn:

Chain WorkManager tasks and pass a file URI between workers.

AI-drafted, machine-checkedSource: developer.android.comintermediate
Chain WorkManager tasks and pass a file URI between workers.

This tests WorkManager chaining and data passing. A strong answer chains Download, Unzip, and Process with WorkContinuation, passes URI via outputData, and reads it with inputData. A red flag is suggesting global state, SharedPreferences, or thread blocking.

WHAT THIS TESTS: The interviewer wants to know if you understand WorkManager beyond simple one-off tasks. Specifically, they are testing two things: first, whether you know how to use WorkContinuation to build dependent chains so that work executes sequentially; second, whether you understand the correct mechanism for passing lightweight data between workers using Data objects rather than external mutable state. At the senior level, they also care if you recognize the constraints of Data, such as the 10 KB size limit, and whether you know how to handle failure propagation in a chain.

A GOOD ANSWER COVERS: A strong response starts by describing a chain created with WorkManager.getInstance(context).beginWith(downloadWorkRequest).then(unzipWorkRequest).then(processWorkRequest).enqueue(). It then explains that the DownloadWorker computes the file URI and packages it into a Data object using Data.Builder, then returns Result.success(outputData). The UnzipWorker receives this payload through its inputData and extracts the URI with inputData.getString(KEY_URI). The candidate should mention that WorkManager guarantees the chain runs in order and that each worker runs only after the previous one succeeds. They should also note that Data is intended for small payloads only and that large files should be referenced by URI or file path rather than being passed as byte arrays.

COMMON WRONG ANSWERS: Red flags include suggesting static variables, SharedPreferences, or an event bus to communicate the URI between steps. These break when the process dies and WorkManager reschedules work. Another mistake is trying to pass the actual file bytes through Data, which will crash or fail due to the 10 KB limit. Some candidates also suggest calling enqueue separately on each WorkRequest without chaining, which destroys the ordering guarantee. Finally, returning Result.failure without explaining how it halts the chain is a gap senior interviewers notice.

LIKELY FOLLOW-UPS: The interviewer may ask what happens if the download worker returns Result.failure. The correct answer is that the chain halts unless you have configured a unique work policy or used an observer to retry. They might also ask how to run unzip and process in parallel after download, which requires using then with a list of OneTimeWorkRequests. Another common follow-up is how to tag workers for cancellation or how to observe chain progress using LiveData or Flow.

ONE CONCRETE EXAMPLE: Imagine downloading a 50 MB zip of user assets. The DownloadWorker writes the file to context.cacheDir and produces outputData with putString("zip_uri", file.toURI().toString()). The UnzipWorker reads inputData.getString("zip_uri"), extracts entries to a folder, and outputs putString("folder_path", folder.getAbsolutePath()). The ProcessWorker reads "folder_path" and uploads parsed metadata. The entire chain is built with beginWith(download).then(unzip).then(process) and observed via WorkManager.getWorkInfosForLiveData to show a notification progress bar.

Source: developer.android.com

Read the original → developer.android.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.