如何立即开始执行 Kotlin Coroutine

How to start executing of Kotlin Coroutine immediately

我想立即启动协程。我有一段代码:

class SampleActivity : AppCompatActivity(), CoroutineScope {

    private var job: Job = Job()
    override val coroutineContext: CoroutineContext
        get() = Dispatchers.Main + job

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        Log.d("SampleActivity", "Before launch")

        launch {
            Log.d("SampleActivity", "Inside coroutine")
        }

        Log.d("SampleActivity", "After launch")
    }
}

输出是:

Before launch
After launch
Inside coroutine

是否可以按如下顺序实现输出?

Before launch
Inside coroutine
After launch

尝试启动它:

launch(Dispatchers.Main.immediate)

this 文章中有更多信息。