Android Work Manager  -  WorkInfo and Update UI

Android Work Manager - WorkInfo and Update UI

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 will 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 met
  • RUNNING -> indicates that work request is currently being executed
  • SUCCEEDED -> 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


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.


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>> =

    fun addNumbers() {
        val addWorker = OneTimeWorkRequestBuilder<SimpleAddWorker>()
        val addBuilder =


In the above code , the following things are handled

  • Create LiveData<List<WorkInfo>> to hold the WorkRequest status
  • getWorkInfosByTagLiveData(TAG_OUTPUT) - will get the WorkInfo based on TAG_OUTPUT(const val TAG_OUTPUT = "output")
  • So , as we can see AddWorker is OneTimeWorkRequestBuilder , it ts tagged by addWorker.addTag(TAG_OUTPUT) and it is enqueued .

Oberver in Actvity

In Activity , lets call viewmodel addNumbers()

viewBinding.buttonAddNumbers.setOnClickListener {
            Log.d("SimpleAddWorker", "buttonAddNumbers")

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()) {

            // 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
            } else {
                Log.d("SimpleAddWorker", "show progress bar")

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 -

In the next article , we will discuss about Periodic WorkManager

Please leave your comments to improve.

Enjoy coding