private lateinit var job: Job override val coroutineContext: CoroutineContext get() = Dispatchers.Main + job + CoroutineExceptionHandler { _, _ -> } launch(Dispatchers.IO) { val response = viewModel.doSomethingAsync() withContext(Dispatchers.Main) { if (response.isValid()) { MyDialog(requireContext()).apply { show() setRandomListener { this@withContext.launch(Dispatchers.IO) { val validationResponse = viewModel.doAnotherThingAsync() withContext(Dispatchers.Main) { dismiss() } } } } } } }