Android WorkManager Pass Parameter (Kotlin)

Jun 11, 2019
class DownloadPlaceWorker(context : Context, params : WorkerParameters) : Worker(context, params) {    companion object {        private const val WORK_NAME = "DownloadPlaceWorker"        private const val PARAM_PARENT_KEY = "parent_key"        fun run(parentKey: String) : LiveData<WorkInfo> {            val data = workDataOf(PARAM_PARENT_KEY to parentKey)            val work = OneTimeWorkRequestBuilder<DownloadPlaceWorker>()                    .setInputData(data)                    .build()            WorkManager.getInstance().enqueueUniqueWork(WORK_NAME, ExistingWorkPolicy.KEEP, work)            return WorkManager.getInstance().getWorkInfoByIdLiveData(work.id)        }    }    override fun doWork(): Result {        val parentKey = inputData.getString(PARAM_PARENT_KEY)        return Result.success()    }}

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.