Work States
As your work goes through its lifetime, it goes through various States. Later in this document, we will talk about how to observe the changes. But first, you should learn about each of them:
- Work is in the
BLOCKEDStateif it has prerequisite work that hasn't finished yet. - Work that is eligible to run as soon as its
Constraintsand timing are met is considered to beENQUEUED. - When a worker is actively being executed, it is in the
RUNNINGState. - A worker that has returned
Result.success()is considered to beSUCCEEDED. This is a terminalState; onlyOneTimeWorkRequests may enter thisState. - Conversely, a worker that returned
Result.failure()is considered to beFAILED. This is also a terminalState; onlyOneTimeWorkRequests may enter thisState. All dependent work will also be marked asFAILEDand will not run. - When you explicitly cancel a
WorkRequestthat hasn't already terminated, it enters theCANCELLEDState. All dependent work will also be marked asCANCELLEDand will not run.
Observing your work
After you enqueue your work, WorkManager allows you to check on its status. This information is available in a WorkInfo object, which includes the id of the work, its tags, its current State, and any output data.
You can obtain WorkInfo in one of three ways:
- For a specific
WorkRequest, you can retrieve itsWorkInfoby theWorkRequestidusingWorkManager.getWorkInfoById(UUID)orWorkManager.getWorkInfoByIdLiveData(UUID). - For a given tag, you can retrieve
WorkInfoobjects for all matchingWorkRequests usingWorkManager.getWorkInfosByTag(String)orWorkManager.getWorkInfosByTagLiveData(String). - For a unique work name, you can retrieve
WorkInfoobjects for all matchingWorkRequests usingWorkManager.getWorkInfosForUniqueWork(String)orWorkManager.getWorkInfosForUniqueWorkLiveData(String)
The LiveData variants of each of the methods allow you to observe changes to the WorkInfo by registering a listener. For example, if you wanted to display a message to the user when some work finishes successfully, you could set it up as follows:
Kotlin
WorkManager.getInstance(myContext).getWorkInfoByIdLiveData(uploadWorkRequest.id)
.observe(lifecycleOwner, Observer { workInfo ->
if (workInfo != null && workInfo.state == WorkInfo.State.SUCCEEDED) {
displayMessage("Work finished!")
}
})
Java
WorkManager.getInstance(myContext).getWorkInfoByIdLiveData(uploadWorkRequest.getId())
.observe(lifecycleOwner, new Observer<WorkInfo>() {
@Override
public void onChanged(@Nullable WorkInfo workInfo) {
if (workInfo != null && workInfo.state == WorkInfo.State.SUCCEEDED) {
displayMessage("Work finished!")
}
}
});

