AsyncTask as kotlin coroutine

后端 未结 5 485
长发绾君心
长发绾君心 2020-12-06 02:50

Typical use for AsyncTask: I want to run a task in another thread and after that task is done, I want to perform some operation in my UI thread, namely hiding a progress bar

5条回答
  •  悲&欢浪女
    2020-12-06 03:17

    First, you have to run coroutine with launch(context), not with runBlocking: https://kotlinlang.org/docs/reference/coroutines/coroutine-context-and-dispatchers.html

    Second, to get the effect of onPostExecute, you have to use

    Activity.runOnUiThread(Runnable) or View.post(Runnable).

提交回复
热议问题