AsyncTask as kotlin coroutine

后端 未结 5 479
长发绾君心
长发绾君心 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:18

    This does not use coroutines, but it's a quick solution to have a task run in background and do something on UI after that.

    I'm not sure about the pros and cons of this approach compared to the others, but it works and is super easy to understand:

    Thread {
        // do the async Stuff
        runOnUIThread {
            // do the UI stuff
        }
        // maybe do some more stuff
    }.start()
    

    With this solution, you can easily pass values and objects between the two entities. You can also nest this indefinitely.

提交回复
热议问题