Launching coroutines inside Alert Dialog Builder

Viewed 38

If I have a suspend function running inside a coroutine and inside that function I build an alertDialog, does this mean that everything inside the builder is running separately from my coroutine? If I launch a coroutine from the same scope there, is it considered nested?

Example code:

    coroutineScope.launch{
        foo()
    }
    suspend fun foo(){
        ....
        AlertDialog.Builder(activity)
            .setOnDismissListener {
                coroutineScope.launch { foo() }
            }.show()
    }

Does the first scope has the second one as nested or the first coroutine finished its job?

1 Answers

The first coroutine scope finishes once the dialog is created and all its children are cancelled, that's why you cannot initiate a suspend function from inside the alert dialog builder. So the second coroutine you're launching is not nested.

Related