'Action android.appwidget.action.APPWIDGET_UPDATE trigger after enqueue a work

I have a case that the user has to schedule work right after the home button was pressed. So I put enqueue code inside "onPaused " method of activity. I also have a simple widget that shows user's to-do list. Every time worker was enqueued, action android.appwidget.action.APPWIDGET_UPDATE was fired and so the onUpdate method of AppWidgetProvider was called. I used workmanager 2.4.0 OnPause method in MainActivity

override fun onPause() {
    super.onPause()
    val syncHealthData = OneTimeWorkRequestBuilder<TestWorker>().addTag("TEST").build()
    WorkManager.getInstance(this)
         .enqueueUniqueWork(1.toString(), ExistingWorkPolicy.REPLACE, syncHealthData)
}

And WidgetProvider file

override fun onUpdate(
    context: Context,
    appWidgetManager: AppWidgetManager,
    appWidgetIds: IntArray
) {
    Log.e("update", "onUpdate") // called upto 2 times
}

I found this issue



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source