Android AsyncTask using kotlin Coroutines
After AsyncTask deprecated on API 29, I was looking for something to replace it with. And i found this soloution using Kotlin Coroutins
on StackOverflow. So i decided to publish it as a library. Maybe someone needs this.
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Then Add the dependency
dependencies {
implementation 'com.github.alibardide5124:AsyncTaskNeo:LEAST_VERSION'
}
You can use this library by typing AsyncTaskNeo.executeAsyncTask<Output, Progress>()
This function needs three input, and all of them are functions... onPreExecute
, doInBackground
, onProgressUpdate
and onPostExecute
.Just like the original AsyncTask.
Here is an example of using:
AsyncTaskNeo.executeAsyncTask<Boolean, Int> (
onPreExecute = {
// Do something before heavy task
},
doInBackground = { publishProgress ->
// Do some heavy works. Dont update ui here. Call publish progress for updating ui.
true // Then return a Boolean (for example)
},
onProgressUpdate = {
// Update ui here
textview.text = it.toString()
},
onPostExecute = {
// Do something on the end
}
)
note: You will get inappropriate blocking method
call warning. But don't mind it. There's no problem.
How was it? I hope you use it if you needs this.
AsyncTaskNeo created by Ali Bardide and it's licensed under a Apache License 2.0.