I am trying to attempt async programming in android by using the WorkManager and Worker classes by getting a worker to change the view of the MainActivity (instead of doing it on the MainActivity itself). Here is my code for MainActivity:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val myWorkRequest: WorkRequest =
OneTimeWorkRequestBuilder<myWorker>()
.build()
WorkManager
.getInstance(this)
.enqueue(myWorkRequest)
}
And this is the code for my Worker class:
class myWorker(appContext: Context, workerParams: WorkerParameters, mainView: View): Worker(appContext, workerParams) {
private val ctx = appContext
private val view = mainView
override fun doWork(): Result {
// Do the work here
myWork()
// Indicate whether the work finished successfully with the Result
return Result.success()
}
fun myWork(): Boolean{
val textView2: TextView = view.findViewById(R.id.textView2)
textView2.setText("TEST!")
return true;
}
}
I am pretty sure the problem lies in the passing of parameters into myWorker, and I don't quite understand how and what exactly I should be passing into the Worker class in order to make it affect the MainActivity.