Android Work Manager - WorkInfo and Update UI

I am an Android application developer who is looking for new challenges to solve , love to learn and implement new things in coding
Please take a look at this post Android Manager Basics which discuss about Work Manager Basics
This blog article discusses about updating Work Manager's result in UI via WorkInfo and ViewModel
- Work Info
- view model
WorkInfo
WorkInfowill have information abut WorkRequest's status , tag and output.
Different current state of WorkRequest as follows
ENQUEUED-> used to indicate 'WorkRequest' is enqueued and ready to run when the constraints are metRUNNING-> indicates that work request is currently being executedSUCCEEDED-> indicates that work request is executed successfully.FAILED-> indicates that work request is not executed successfully .BLOCKED-> indicated that work request is currently blocked because prerequisites are not meant.CANCELLED-> indicated that work request hss been cancelled and will not execute.
There is a simpler method to know the WorkRequest state which is
workInfo.state.isFinished
The above method will return true if the WorkRequest is SUCCEEDED or FAILED or CANCELLED
So we can use this method to get the WorkRequest status and update the UI.
ViewModel
We will use the LiveData to hold a WorkInfo object to get the updated state and output from work.
class SimpleAddViewModel(application: Application) : AndroidViewModel(application) {
private val workManager = WorkManager.getInstance(application)
internal val workInfos: LiveData<List<WorkInfo>> =
workManager.getWorkInfosByTagLiveData(TAG_OUTPUT)
fun addNumbers() {
val addWorker = OneTimeWorkRequestBuilder<SimpleAddWorker>()
addWorker.addTag(TAG_OUTPUT)
val addBuilder = addWorker.build()
workManager.beginWith(addBuilder).enqueue()
}
}
In the above code , the following things are handled
- Create
LiveData<List<WorkInfo>>to hold theWorkRequeststatus getWorkInfosByTagLiveData(TAG_OUTPUT)- will get theWorkInfobased on TAG_OUTPUT(const val TAG_OUTPUT = "output")- So , as we can see AddWorker is
OneTimeWorkRequestBuilder, it ts tagged byaddWorker.addTag(TAG_OUTPUT)and it is enqueued .
Oberver in Actvity
In Activity , lets call viewmodel addNumbers()
viewBinding.buttonAddNumbers.setOnClickListener {
Log.d("SimpleAddWorker", "buttonAddNumbers")
viewModel.addNumbers()
}
Now lets check observer code
viewModel.workInfos.observe(this, Observer { workInfoList ->
// check the workInfoList is null ot empty and
// if it empty then simply return
if (workInfoList.isNullOrEmpty()) {
return@Observer
}
// get the workINfo from workInfoList
val workInfo = workInfoList[0]
// now check workInfo's status and update UI
if (workInfo.state.isFinished) {
Log.d("SimpleAddWorker", "hide progress bar")
val totalCount = "Total : ${workInfo.outputData.getInt(TOTAL_COUNT, 0).toString()}"
viewBinding.textViewResult.text = totalCount
hideProgressBar()
} else {
Log.d("SimpleAddWorker", "show progress bar")
showProgressBar()
}
})
}
In above code , workInfo.state.isFinished is used to check the WorkRequest state and if it returns true , the result from the Worker is obtained by workInfo.outputData.getInt(TOTAL_COUNT, 0).toString()
Sample code - https://github.com/vprabhu/WorkManagerVMBasics
In the next article , we will discuss about Periodic WorkManager
Please leave your comments to improve.
Enjoy coding



