Search code examples
androidandroid-workmanagerandroid-thread

Sync/Work option for foreground and background using WorkManger


Can WorkManager be used to handle both immediate/foreground task AND background task or should I use 2 separate options?

Currently app uses background Thread using application context to make calls to a few methods to run for uploading/syncing data with API. The user is shown a visual indication in app of progress and when it's done.

Would like to add same/similar functionality in background state periodically.

Note - Android 12 target restrict starting foreground from background: https://developer.android.com/about/versions/12/foreground-services


Solution

  • You can but you should not because it is too complicated. Just use WorkManager for work that should run when it is in the background. For immediate syncing, just use a Coroutine or similar and launch the job you want, which is easier because you can keep the user updated about the sync status. Keep it simple.